Kevin Schmidt Kevin Schmidt - 21 days ago 8
Android Question

app crashes when it starts after defining onClick method of an Actionbar Icon

hey stack overflow people,

i just started programming android apps a few days ago and now i have a problem when i want to build an app with multiple activities:

basically i want to get to my settings activity by clicking on the icon i implemented in the Actionbar.
I set the onClick attribute of this icon to startSettings

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


in my (menu) main.xml file ...

but when i do this, the app crashes when i want to start it.
the weird thing is, when I start the method with a normal button in my activity_main.xml file, it works fine ... So i think the problem is in the main.xml file. it says in this file that the method startSettings in 'MainActivity' has incorrect signature. and "Checks if the method specified in onClick XML attribute is declared in related activity"
But i don't know what that means ...:/

hope someone can help me.
if you need some other code to help, just say it.

greetings

kevin

here is the error in my console

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kevs272.testing, PID: 31225
Theme: themes:{default=overlay:com.ashok.nougatCM, fontPkg:com.ashok.nougatCM, com.android.systemui=overlay:com.ashok.nougatCM, com.android.systemui.navbar=overlay:com.cyngn.hexo}
android.view.InflateException: Couldn't resolve menu item onClick handler startSettings in class com.example.kevs272.testing.MainActivity
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:244)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:444)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:479)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:196)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
at com.example.kevs272.testing.MainActivity.onCreateOptionsMenu(MainActivity.java:23)
at android.app.Activity.onCreatePanelMenu(Activity.java:2852)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:360)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:331)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1353)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1633)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:132)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


and this is my (menu) main.xml file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:title="Settings"
android:icon="@drawable/ic_settings_white_24dp"
app:showAsAction="always"
android:onClick="startSettings" />

<item
android:id="@+id/action_about"
android:title="about"
android:icon="@drawable/ic_info_outline_white_24dp"
app:showAsAction="always"
/>



Answer

i recomend you to put the function in the on onMenuItemSelected of the activity like this:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_settings:
        startSettings();
        return true;
    }
    return super.onMenuItemSelected(featureId, item);
}

and remove the view param