I read some posts about how could I call an Android's activity from another Java Class implemented in the application, but no answer to my problem.
I have a connection class (Connection.java
) which handle the permanent connection needed by the application.
This one is constructed with Singleton pattern, so everytime I need connection information or request something I do:
final Connection conn = Connection.getConnection(getApplicationContext());
//... Some Code Here
conn.methodDoSomethingA();
Then, I have an TabActivity which 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);
//... same for activities B, C, D and E
tabHost.setCurrentTab(0);
}
}
Now, I have a public method in Connection class to end connection - endConnection()
- which is called several times within the Connection class, for example, when there is Socket Timeout or when receiving a custom message from server informing to end session.
The problem starts here - when endConnection()
is called it must close sockets and then show a Activity (Theme.Dialog) informing the connection is lost.
In order to achieve that 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() {
// ... Close sockets and streams - SOME CODE HERE
// Show Disconnect Dialog
Intent myIntent = new Intent(appContext, Disconnect.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity( myIntent );
}
}
I also tried to pass the TabActivity
context as argument to the Connection.java
class and used it instead of appContext
, but without success either.
I'm getting this 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?!