0

I've create an menu (my toolbar) and an item inside of it (Settings)

menu.xml code:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.sergio.testecalc.MainActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:onClick="sendMessage"
    android:title="@string/config"
    app:showAsAction="never" />

I want to link the onClick with Settings_Activity.java, then I put this on Main_Activity.java:

public void sendMessage(View view)
{
    Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
    startActivity(intent);
}

But Android Studio shows me that:

Method sendMessage in MainActivity has incorrect signature

enter image description here

Why? Whats wrong with my code? Sorry for my bad english :(

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Sérgio LP
  • 19
  • 1
  • 4

1 Answers1

0

To fix your issue, change View to MenuItem like so:

public void sendMessage(MenuItem mItem)
{
    Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
    startActivity(intent);
}

See here for more details.

Note that it's more common to use onCreateOptionsMenu() and onOptionsItemSelected(), see here for details.

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137