4

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?!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jorge
  • 695
  • 2
  • 9
  • 21
  • You wanna intent an Activity form TabActivity ? – iSun Dec 14 '11 at 14:50
  • did you declare all your activities on your Manifest file ? plz add your StackTrace of the error you got – Houcine Dec 14 '11 at 14:58
  • @Ali I want to intent a Activity (anywhere in the application) from Connection class (which is not Activity) – Jorge Dec 14 '11 at 15:41
  • @Houcine All activities are in Manifest file, and I can start if I intent it from another activity. – Jorge Dec 14 '11 at 15:44
  • If I'm reading your code right, appContext will be null at newIntent(appContext...). Typically network communication in an Android application is handled in an AsyncTask (or some equally convenient utility for asynchronous processing, but probably AsyncTask more often than not) which includes a callback mechanism (onPostExecute) to the Activity which started it. Would a model like that suit your purposes? – Phillip Fitzsimmons Dec 14 '11 at 15:45
  • check if your appContext is null just before you call "Intent myIntent = new Intent(appContext, Disconnect.class);". Otherwise it seems you're familiar with how to call Activity from another class. – hovanessyan Dec 14 '11 at 15:48
  • @PhillipFitzsimmons The appContext is not null. It is passed as an argument - `getApplicationContext()` - in Connection constructor. – Jorge Dec 14 '11 at 16:19
  • @hovanessyan I forced check if appContext is null by putting the code within blocks `if (appContext!=null){ // ...SOME CODE HERE }` and the problem still the same. – Jorge Dec 14 '11 at 16:21
  • well, apparently something IS null. Can you place longer stacktrace and use the formatting to place it nicely in your question body? – hovanessyan Dec 14 '11 at 16:26
  • !The whole! starcktrace please. – hovanessyan Dec 14 '11 at 17:05
  • @hovanessyan I cant catch the null exception in endConnection() code. I'll try to find what's null. – Jorge Dec 14 '11 at 17:19
  • you should have "caused by" in your stacktrace. – hovanessyan Dec 14 '11 at 17:23

1 Answers1

4

I found the error.

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

Apparently I did everything ok, and this is how it is done!

The error was a newbie mistake that embarasses me:

The Disconnect.java had a listener to a button that did not exists in his Content View XML layout file, but it exists in another layout!

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

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

  1. call an activity from another java class;
  2. don't post questions without exploring unthinkable mistakes!!!
Jorge
  • 695
  • 2
  • 9
  • 21