onCreateOptionsMenu时失败

时间:2017-07-20 04:48:03

标签: java android xml menu

我是新手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?是我的代码错了或我忘记插入的东西? 谢谢你

2 个答案:

答案 0 :(得分:2)

MainActivityandroid:onClick="doAction"没有相应的处理程序,因此您可以添加

public class MainActivity extends AppCompatActivity {

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

注意:supports API Level 11或更高版本使用onOptionsItemSelected

答案 1 :(得分:1)

  

android.view.InflateException:无法解析onClick上的菜单项   班级doAction

中的处理程序com.simasjiwa.testingheadermenu.MainActivity

doAction()文件中没有MainActivity.java方法。

<强> SOLUTION:

#。android:onClick="doAction"删除属性<item>

更新main_menu.xml,如下所示:

<?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"
        app:showAsAction="always"
        android:id="@+id/testing">
    </item>
</menu>

#。覆盖onOptionsItemSelected()并在选择特定菜单item时执行某些操作。

更新MainActivity.java,如下所示:

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);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.testing) {

            doAction();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void doAction() {
        // Do something...
        Toast.makeText(getApplicationContext(), "Clicked!", Toast.LENGTH_SHORT).show();
    }
}