InflateException:无法解析的菜单项onclick处理程序 [英] InflateException: Couldn't resolve menu item onClick handler

查看:331
本文介绍了InflateException:无法解析的菜单项onclick处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XML中定义的菜单项,并尝试使用添加的API 11.当活动是在模拟器中运行4.0.3推出了onclick属性,但下列情况除外发生:

 致命异常:主要
android.view.InflateException:无法解析的菜单项onclick处理程序
    onFeedbackMenu类android.view.ContextThemeWrapper

...
java.lang.NoSuchMethodException:产生的原因onFeedbackMenu
    [接口com.actionbarsherlock.view.MenuItem]
在java.lang.Class.getConstructorOrMethod(Class.java:460)
 

我不明白是什么原因造成的异常,因为下面的方法在我的活动定义

 进口com.actionbarsherlock.view.MenuItem;
...
公共无效onFeedbackMenu(菜单项菜单项){
    Toast.makeText(这一点,onFeedBack,Toast.LENGTH_LONG).show();
}
 

我的XML菜单定义文件包含:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
...
    <项目
        机器人:ID =@ + ID / menu_feedback
        机器人:图标=@可绘制/ ic_action_share
        机器人:showAsAction =ifRoom
        机器人:标题=@字符串/ menu_feedback
        机器人:的onClick =onFeedbackMenu/>
< /菜单>
 

有关向后兼容性我使用ActionBarSherlock,也得到了非常相似的异常,当我在2.3.x运行的应用程序。

这是一个更完整的版本的堆栈跟踪

 致命异常:主要
android.view.InflateException:无法解析的菜单项onclick处理程序
    onFeedbackMenu类android.view.ContextThemeWrapper
    在com.actionbarsherlock.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:204)
    在com.actionbarsherlock.view.MenuInflater $ MenuState.setItem(MenuInflater.java:410)
    在com.actionbarsherlock.view.MenuInflater $ MenuState.addItem(MenuInflater.java:445)
    在com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:175)
    在com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:97)
    ...
java.lang.NoSuchMethodException:产生的原因onFeedbackMenu
    [接口com.actionbarsherlock.view.MenuItem]
    在java.lang.Class.getConstructorOrMethod(Class.java:460)
    在java.lang.Class.getMethod(Class.java:915)
    在com.actionbarsherlock.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:202)
    ... 23更多
 

解决方案

我发现,工作了我一个解决方案。 通常在布局的的onClick 属性具有以下方法

 公共无效方法名(查看视图){
    //行动
}
 

在一个菜单项(在这种情况下福尔摩斯菜单)应遵循以下签名:

 公共布尔方法名(菜单项项){
    //行动
}
 

所以,在你的方法返回您的问题无效,而不是布尔

I'm defining menu items in XML, and trying to use the onClick attribute that was added in API 11. When the Activity is launched in an emulator running 4.0.3, the following Exceptions occur:

FATAL EXCEPTION: main
android.view.InflateException: Couldn't resolve menu item onClick handler 
    onFeedbackMenu in class android.view.ContextThemeWrapper

...
Caused by: java.lang.NoSuchMethodException: onFeedbackMenu 
    [interface com.actionbarsherlock.view.MenuItem]
at java.lang.Class.getConstructorOrMethod(Class.java:460)

I don't understand what is causing the Exception, since the following method is defined in my Activity

import com.actionbarsherlock.view.MenuItem;
...
public void onFeedbackMenu( MenuItem menuItem ) { 
    Toast.makeText( this, "onFeedBack", Toast.LENGTH_LONG ).show();
}

My XML menu definition file contains:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
...
    <item
        android:id="@+id/menu_feedback"
        android:icon="@drawable/ic_action_share"
        android:showAsAction="ifRoom"
        android:title="@string/menu_feedback"
        android:onClick="onFeedbackMenu" />
</menu>

For backwards compatibility I am using ActionBarSherlock, and also getting a very similar Exception when I run the App on 2.3.x.

This is a more Complete version of the Stack trace

FATAL EXCEPTION: main
android.view.InflateException: Couldn't resolve menu item onClick handler 
    onFeedbackMenu in class android.view.ContextThemeWrapper
    at com.actionbarsherlock.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:204)
    at com.actionbarsherlock.view.MenuInflater$MenuState.setItem(MenuInflater.java:410)
    at com.actionbarsherlock.view.MenuInflater$MenuState.addItem(MenuInflater.java:445)
    at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:175)
    at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:97)
    ...
Caused by: java.lang.NoSuchMethodException: onFeedbackMenu 
    [interface com.actionbarsherlock.view.MenuItem]
    at java.lang.Class.getConstructorOrMethod(Class.java:460)
    at java.lang.Class.getMethod(Class.java:915)
    at com.actionbarsherlock.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:202)
    ... 23 more

解决方案

I found a solution that worked for me. Usually the onClick attribute in a layout has the following method

public void methodname(View view) { 
    // actions
}

On a menu item (in this case Sherlock menu) it should follow the following signature:

public boolean methodname(MenuItem item) { 
    // actions
}

So, your problem was that your method returned void and not boolean.

这篇关于InflateException:无法解析的菜单项onclick处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆