문제

I have a beginners 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 INSTANTIATED 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.

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top