gpt4 book ai didi

java - onCreateOptionsMenu 时失败

转载 作者:行者123 更新时间:2023-12-01 20:19:17 33 4
gpt4 key购买 nike

我是新手 Android 程序员,所以我想为我的应用程序制作菜单,但当我尝试将其启动到 AVD 时出现一些错误这是我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
}

这是我的 XML 主目录

<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.simasjiwa.testingheadermenu.MainActivity">

这是我尝试创建的菜单:

  <?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:icon="@mipmap/ic_launcher"
android:title="clickhere"
android:onClick="doAction"
app:showAsAction="always"
android:id="@+id/testing"></item>

</menu>

这是我的 logcat 错误:

FATAL EXCEPTION: main
Process: com.simasjiwa.testingheadermenu, PID: 8322
android.view.InflateException: Couldn't resolve menu item onClick handler doAction in class com.simasjiwa.testingheadermenu.MainActivity
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:248)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:448)
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:483)
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:200)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:122)
at com.simasjiwa.testingheadermenu.MainActivity.onCreateOptionsMenu(MainActivity.java:17)
at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1368)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1648)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:140)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: doAction [interface android.view.MenuItem]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getMethod(Class.java:857)
at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:244)
at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:448) 
at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:483) 
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:200) 
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:122) 
at com.simasjiwa.testingheadermenu.MainActivity.onCreateOptionsMenu(MainActivity.java:17) 
at android.app.Activity.onCreatePanelMenu(Activity.java:2538) 
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362) 
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98) 
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335) 
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1368) 
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1648) 
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:140) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5017) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
at dalvik.system.NativeStart.main(Native Method) 

我的问题是,为什么我可以跑到AVD?我的代码是错误的还是我忘记插入的东西?先谢谢了

最佳答案

您的 MainActivity 中没有针对 android:onClick="doAction" 的相应处理程序,因此您可以添加它

public class MainActivity extends AppCompatActivity {

// fields
// other methods
public void doAction(MenuItem item){
// perform your action here
}
}

注意:它 supports API Level 11或以上,否则使用 onOptionsItemSelected

关于java - onCreateOptionsMenu 时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45205398/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com