views:

53

answers:

1

Hello, I have a noob problem. Here is my situation:

I want to start a new activity from the main activity. The code to launch the new activity is found in a separate class file. I seem to be passing the wrong arguments and I am ending up in a nullpointerexception when trying to launch the new activity. The new activity launches fine when I place the code in the main activity class file, therefore the second activity and the manifest are fine. Here is a sample of my code:

In my main activity class where I instanciate the second class (THIS IS MY MAIN ACTIVITY. I OMITTED THE REST BECAUSE I DO NOT THINK IT IS RELATED TO THE PROBLEM):

Tester mytest = new Tester();
mytest.test(this);

In my second class file (THIS IS NOT AN ACTIVITY; iT IS A CLASS THAT IS INTANCIATED IN THE ACTIVITY):

public class Tester extends Activity {
     Intent myIntent;
     public void test (Context context) {
               myIntent = new Intent (Intent.ACTION_VIEW);
               myIntent.setClass(context, newActivity.class);
               thebutton.setOnClickListener(
            new OnClickListener() {  
                public void onClick(View v) { 
                    startActivity(myIntent);
                }  
            }       
        ):}

When I perform the click I receive a nullpointerexception at startactivity. Can anyone enlighten me on this please?I am sure that I am wrongly using the context.

Thanks

A: 

Activities are started with Intents. Please read the Android Application Fundamentals first and try the Hello World app :)

I understood that you will use your separate Tester class at all cost ;) so I'm trying to adapt and help you out there.

First of all, don't let your class inherit from Activity. This won't help you, cause this calls will probably not have any valid context. Activity somehow implements the template pattern, providing you key method like onCreate(...), onPause(...) etc and is instantiated by the Android OS.

If you still want to use the class, you have to pass in the context. Probably you're aiming for some MVC/MVP pattern structure, anyway.

public class Tester {
    private Context context;
    public Tester(Context context){
        this.context = context;
    }

    public void test () {
       final Intent myIntent = new Intent(context, NewActivity.class);

       //guess this comes from somewhere, hope through a findViewById method
       thebutton.setOnClickListener(
              new OnClickListener() {  
                public void onClick(View v) { 
                    context.startActivity(myIntent);
                }  
              }       
        )};
    }
}

This would be a proposed solution from my side. A problem I still see here is on how you retrieve the button in that test() method. In order to have that work properly you have to retrieve it from some View class (with view.findViewByid(R.id.myButton)) or to create it dynamically and associate it with the view during the onCreate(...) of your Activity (probably using an Inflater).

Juri
The onclick(view v) is found in a separate class file. do I have to extend the class to activity to be able to use startactivity or theres another way?
Yash
You will need a Context to use startActivity: http://goo.gl/Wp60
Juri
Just for curiousity: what's the reason of having the onClick event in a separate class? In your above code: I wouldn't extend from Activity, but rather if you need so, pass the Context of the activity in the constructor of your Tester class and then use the context to invoke actions like startActivity(..) etc.
Juri
mytest.test(this); - I am passing the context of the main activity to the class here and using it in my intent, is this part correct? Thank you for trying to help Juri
Yash
And I am using the onclick in a class because what is displayed on load depends on a condition which is sorted out in the class.
Yash
heavily modified my post. Take a look, it should work for you now. You need to execute the startActivity from a valid context.
Juri
I omitted the part where I created the button dynamically.. in fact the onClick is on a linearLayout ... i just placed it here this way for sake of simplicity. And it worked. I did not set the context where startactivity should work. Thanks for your advice Juri, really appreciated.
Yash

related questions