How to start Activity from java class?

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()); //... Some Code Here conn.methodDoSomethingA(); 

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); //... same for activities B, C, D and E tabHost.setCurrentTab(0); } } 

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() { // ... 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 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 ?!

+4
source share
1 answer

I found a mistake.

First of all, I want to thank all of you for your comments.

Apparently, I did everything right, and so it is done!

The error was a beginner's mistake, which confuses me:

Disconnect.java had a button listener that did not exist in its Content View XML format layout, but it exists in a different layout!

I never suspected it! Stupid, isn't it?

I hope this post can help everyone for one of two things:

  • calling a function from another java class ;
  • Do not send questions without exploring unthinkable errors !!!
+4
source

Source: https://habr.com/ru/post/1386342/


All Articles