我在一个简单的hello world应用程序中添加了一个菜单!但是我的应用程序一直在崩溃。 我没有添加我的xml代码。我的xml文件名是res中菜单文件夹内的菜单。 这是我的java文件。
MyActivity.java
public boolean onCreateOptionsMenu(Menu menu1){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu1); //line 19
return true;
}
Logcat
06-03 18:03:57.682 13542-13542/com.example.kaushalraj.a424 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kaushalraj.a424, PID: 13542
android.view.InflateException: Couldn't resolve menu item onClick handler action in class com.example.kaushalraj.a424.MainActivity
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:253)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NoSuchMethodException: action [interface android.view.MenuItem]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:249)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:481)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:529)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:205)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:127)
at com.example.kaushalraj.a424.MainActivity.onCreateOptionsMenu(MainActivity.java:19)
at android.app.Activity.onCreatePanelMenu(Activity.java:3388)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:364)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1377)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1657)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:0)
我不知道你的情况(以及我没有评论的声誉)但是:你使用的是什么应用主题?如果您使用的主题顶部没有操作栏,则当您尝试将菜单附加到该主题时会出现此错误,因为操作栏不存在。
您有两种解决方案:
如果您使用的是 Theme.AppCompat.Light.NoActionBar ,请检查res / values / styles.xml中的主题,您必须将其更改为 Theme.AppCompat之类的内容.Light.DarkActionBar 。这会将操作栏添加到您的活动中。
使用支持工具栏库,将工具栏添加到活动xml布局,然后从活动类中获取OnCreate()中的视图并使用setSupportActionBar(myToolbar)
。这会将工具栏视图设置为将附加菜单的操作栏。