Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to start an Activity from a java class?
    primarykey
    data
    text
    <p>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.</p> <p>I have a connection class (<code>Connection.java</code>) 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:</p> <pre><code>final Connection conn = Connection.getConnection(getApplicationContext()); //... Some Code Here conn.methodDoSomethingA(); </code></pre> <p>Then, I have an TabActivity which contains 5 Activities (A, B, C, D, E):</p> <pre><code>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); } } </code></pre> <p>Now, I have a public method in Connection class to end connection - <code>endConnection()</code> - 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.</p> <p>The problem starts here - when <code>endConnection()</code> 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:</p> <pre><code>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 ); } } </code></pre> <p>I also tried to pass the <code>TabActivity</code> context as argument to the <code>Connection.java</code> class and used it instead of <code>appContext</code>, but without success either.</p> <p>I'm getting this errors:</p> <pre><code>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 </code></pre> <p>In other words: - How to start an Activity from a java class?!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload