New nullpointerexception activity

I have a problem with newbies. Here is my situation:

I want to start a new activity from the main action. The code for starting a new action is in a separate class file. I seem to pass the wrong arguments, and I get a nullpointer exception when trying to start a new action. The new action starts fine when I put the code in the main file of the activity class, so the second action and manifest are great. Here is an example of my code:

In my main class of activity, where I intrude into the second class (THIS IS MY MAIN ACTIVITY. I REFUSED REST, BECAUSE I DO NOT THINK, IT RELATES TO THE PROBLEM):

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

In my second class file (THIS IS NOT ACTIVITY: THIS IS A CLASS SPECIFIED 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);
                }  
            }       
        ):}

, nullpointerexception startactivity. - , ? , .

+3
1

. Android Android Hello World:)

, Tester ;), .

, , Activity. , , , . - , , onCreate(...), onPause(...) .. Android.

, . , MVC/MVP.

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);
                }  
              }       
        )};
    }
}

. , , - , test(). , View ( view.findViewByid(R.id.myButton)) onCreate(...) (, Inflater).

+4

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


All Articles