I have seen this tutorial on YouTube that is about how to open a new activity from android button click.
Here's a link to the video.
I create the button, check the button ID and use the following code to do it.
public Button button2;
public void start() {
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Number2.this, Number3.class));
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_number2);
start();
}
I have the following classes: Number1.java, Number2.java and Number3.java.
Opening a new activity from Number1 to Number2 and it works fine, however, when I try to open the third activity it gives me this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{thisapp.thisapplication/thisapp.thisapplication.Number2}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
I can't seem to figure out what I'm doing wrong.