1

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!

Rucha Bhatt Joshi
  • 822
  • 1
  • 15
  • 38
Daan Seuntjens
  • 880
  • 1
  • 18
  • 37
  • 1
    Try this. Create another method with another name, like `goToMenu()`. and put your intent call inside that method and change in xml to `onClick="goToMenu()"` – SripadRaj Aug 04 '17 at 12:09

2 Answers2

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);
}
Anmol317
  • 1,356
  • 1
  • 14
  • 31
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);


            }
        });
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
  • 2
    what he want is different and what you are suggesting is different. His goal can be achieved by using `android:onClick="menu"` – Anand Singh Aug 04 '17 at 12:14
  • Thanks for your answer but the error was comming from my activity that wasen't named correctly, but thanks a lot for your time :) – Daan Seuntjens Aug 04 '17 at 12:27