[Solved] Button not working (android studio) [duplicate]


try this way

 Intent i = new Intent(MainActivity.this, Main2Activity.class);
 startActivity(i);

EDIT: I wanted to add a bit of an explanation to help

Hi MRAK,

First things first, you do NOT need to create an onClick listener. Remove all of that completely. Android studio is a beast and automatically does that for you using the XML file. When you set “onClick” in the XML file, it automatically calls the name of whatever method you put in there. You should change it so it is not also called “onClick.” I would prefer to call it “startAcitivty2” or so on so you are not confused later. I stuck with your method name for now.

See below for corrected code:

public void onClick(View v){
    // note in the below line i'm just using "this"
    Intent myIntent = new Intent(this, Main2Activity.class)
    // Secondly, you need to end the current activity
    finish();
    // Third, you need to start your new activity... 
    //    Creating an Intent does not the activity alone
    startActivity(myIntent);
}

Also, this has so many downvotes because this has been asked 1000+ times. Please use google or the search bar above before asking. Google will reroute you to stackoverflow anyway 🙂

4

solved Button not working (android studio) [duplicate]