1

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Possible duplicate of [Android - Null pointer Exception - findViewById()](http://stackoverflow.com/questions/19078461/android-null-pointer-exception-findviewbyid) – OneCricketeer Oct 15 '16 at 19:29
  • i think your button2 is null. Can you post the layout file? And also the part of function where you are going setContentView in activity Number2 – Zaartha Oct 15 '16 at 19:30
  • Basically, whatever `R.layout` value you have within `setContentView` does not contain the ID for the button that you tried to use `findViewById` for – OneCricketeer Oct 15 '16 at 19:30
  • @cricket_007 post that as an answer as well :) – KISHORE_ZE Oct 15 '16 at 19:36
  • @KISHORE_ZE I'd rather not. That functionality of `findViewById` is well documented. – OneCricketeer Oct 15 '16 at 19:40
  • Thanks for the help everybody! Problem fixed! It was the button ID. – Norman Soto Oct 15 '16 at 19:42
  • @cricket_007 true, but for other people who are new to stackoverflow and 'somehow' find this question will find a checked answer much easier to get a solution than read comments. – KISHORE_ZE Oct 15 '16 at 19:42
  • You should put the whole code of the two classes, the layouts and the manifest, or I would recommend trying out more tutorials, as you can start a new activity in more ways (try the new boston ;)) – Claudiu Haidu Oct 15 '16 at 19:43
  • @KISHORE_ZE It's not really an answer, though, without more information (the layouts and the Number3 class, maybe). It is just a well-informed guess to the problem. – OneCricketeer Oct 15 '16 at 19:44
  • @NormanSoto wait a sec. How did it even run if the button Id was wrong? Did you put the id of another view – KISHORE_ZE Oct 15 '16 at 19:45
  • @ClaudiuHaidu I would rather recommend he not post huge chunks of code. No offence. Probably the relevant code from the 1st activity and the layout file alone should be enough I feel. Also the newboston is pretty good as well as Derek banas, etc. And of course the official android developers site – KISHORE_ZE Oct 15 '16 at 19:47
  • @cricket_007 True. Np. And apparently it was the id. Not sure how it ran unless he mismatched the Id's which seems unlikely... – KISHORE_ZE Oct 15 '16 at 19:49

0 Answers0