I read several posts on how I can name Android activity from another Java class implemented in the application, but did not answer my problems.
I have a connection class ( Connection.java ) that handles the persistent connection required by the application. It is built with the Singleton template, so every time I need connection information or a query about what I am doing:
final Connection conn = Connection.getConnection(getApplicationContext());
Then I have a TabActivity that contains 5 activities (A, B, C, D, E):
public class Tab extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab); final Connection conn = Connection.getConnection(getApplicationContext()); intent = new Intent().setClass(this, A.class); spec = tabHost.newTabSpec("A") .setIndicator("A", res.getDrawable(R.drawable.tab_A)) .setContent(intent); tabHost.addTab(spec);
Now I have an open method in the Connection class to end the connection - endConnection() - which is called several times in the Connection class, for example, when there is a Socket Timeout or when you receive a special message from the server informing the session about this.
The problem starts here - when endConnection() is called, it should close the sockets, and then show an Activity (Theme.Dialog) indicating that the connection was lost. To achieve this, I did this without success:
public class Connection { private static Connection connection = null; private Context appContext = null; private Connection(Context appContext) { this.appContext = appContext; } public static Connection getConnection(Context appContext) { if (connection == null) return connection = new Connection(appContext); else return connection; } public void endConnection() {
I also tried passing the TabActivity context as an argument to the Connection.java class and used it instead of appContext , but without success either.
I get the following errors:
W/dalvikvm(9449): threadid=3: thread exiting with uncaught exception (group=0x2aaca228) E/AndroidRuntime(9449): Uncaught handler: thread main exiting due to uncaught exception E/AndroidRuntime(9449): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Disconnect}: java.lang.NullPointerException
In other words: - How to start an Activity from a java class ?!