creating new intent error

时间:2017-08-04 12:05:06

标签: android android-intent

I want to open a new activity when i click on an imageview.

MainActivity:

public void menu (View view) {
    Intent i = new Intent(this, Menu.class);
    startActivity(i);
}

Menu:

public class Menu extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
    }
}

When I run the app and i click on the imageview the app crashes and gives the error:

java.lang.IllegalStateException: Could not execute method for android:onClick

As these answeres suggest:

error on creating new intent

Creating new activity, Intent error

P.S I changed my code but it still gave the same error.this seems like a very stupid mistake of mine.Can anyone help me to find my mistake? Thanks in advance!

2 个答案:

答案 0 :(得分:1)

First of all change the name of your activity to MenuActivity from Menu and then in your MainActivity do it like this

public void menu (View view) {
Intent i = new Intent(MainActivity.this, MenuActivity.class);
startActivity(i);
}

答案 1 :(得分:0)

First remove this line from ImageView tag in xml;

android:onClick="menu"

and then You can try below code;

  ImageView btn = (ImageView )findViewById(R.id.btn );
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(myIntent);


            }
        });