InflateException:无法解析onClick on Class上的菜单项

时间:2014-12-15 10:03:06

标签: android

我收到错误:android.view.InflateException: Couldn't resolve menu item onClick handler onClickEnterData in class com.android.timesheet.NewProjects

我在菜单项中使用过onClick操作。我在NewProjects.java中提到错误行

堆栈跟踪:

12-15 04:45:51.845: E/AndroidRuntime(2777): FATAL EXCEPTION: main
12-15 04:45:51.845: E/AndroidRuntime(2777): Process: com.android.timesheet, PID: 2777
12-15 04:45:51.845: E/AndroidRuntime(2777): android.view.InflateException: Couldn't resolve menu item onClick handler onClickEnterData in class com.android.timesheet.NewProjects
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:217)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:417)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:451)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.MenuInflater.parseMenu(MenuInflater.java:188)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.MenuInflater.inflate(MenuInflater.java:110)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at com.android.timesheet.NewProjects.onCreateOptionsMenu(NewProjects.java:82)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.Choreographer.doFrame(Choreographer.java:543)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.os.Handler.handleCallback(Handler.java:733)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.os.Looper.loop(Looper.java:136)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at java.lang.reflect.Method.invokeNative(Native Method)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at java.lang.reflect.Method.invoke(Method.java:515)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at dalvik.system.NativeStart.main(Native Method)
12-15 04:45:51.845: E/AndroidRuntime(2777): Caused by: java.lang.NoSuchMethodException: onClickEnterData [interface android.view.MenuItem]
12-15 04:45:51.845: E/AndroidRuntime(2777):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at java.lang.Class.getMethod(Class.java:857)
12-15 04:45:51.845: E/AndroidRuntime(2777):     at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:215)
12-15 04:45:51.845: E/AndroidRuntime(2777):     ... 22 more

main.xml中:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
     >

    <item
        android:id="@+id/add"
        android:icon="@drawable/ic_action_add_alarm"
        android:title="@string/add_task"
        android:showAsAction="always"
        android:onClick="onClickEnterData"
        />

</menu>  

NewProjects.java:

public void onClickEnterData(View btnAdd) {

    startActivityForResult(new Intent(this, EnterDataActivity.class),
            ENTER_DATA_REQUEST_CODE);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);  ----->82nd Error line
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
//  case R.id.add:
//      Intent intent = new Intent(this, ActionBarButton.class);
        // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        // Intent.FLAG_ACTIVITY_SINGLE_TOP);
//      startActivity(intent);
//      return true;

      case android.R.id.home: 
            onBackPressed();
            return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}  

new_project.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#A6CAED"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list_data"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

1 个答案:

答案 0 :(得分:8)

onClickEnterData的签名是错误的。它必须是

public void onClickEnterData(MenuItem item)

来自文档

  

提示:Android 3.0增加了定义点击的功能   XML中的菜单项的行为,使用android:onClick属性。   属性的值必须是由其定义的方法的名称   使用菜单的活动。该方法必须公开并接受a   单个MenuItem参数 - 当系统调用此方法时,它会通过   选择的菜单项。有关更多信息和示例,请参阅   菜单资源文档。