问题描述
我有一个奇怪的问题,在Android的API 15.一个简单的视图在此视图我有一个简单的菜单有2个菜单项。
这code正常工作与其他Android API,而不是在这款手机采用Android 4.0.3:
I've a strange problem with a simple view in Android API 15. In this view I've a simple menu with 2 menu items.This code works fine with other Android API but not in this phone with Android 4.0.3:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/root_menu"
android:icon="@android:drawable/ic_menu_help"
android:showAsAction="always"
android:title="Help">
<menu>
<item
android:id="@+id/menu_about"
android:onClick="aboutDialog"
android:showAsAction="never"
android:title="About"/>
</menu>
</item>
</menu>
这是活动
public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
/**
* Crea l'action bar
*
* @param menu
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_actionbar, menu);
return true;
}
/**
* Apertura del dialog box con le informazioni sulla versione del programma
*
* @param v
*/
public void aboutDialog(MenuItem v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Test")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
builder.create().show();
}
}
所以没有什么奇怪的这个code。但是,当我尝试在设备上运行我有此异常:
So nothing strange in this code. But when I try to run on the device I've this exception:
android.view.InflateException: Couldn't resolve menu item onClick handler
aboutDialog in class android.view.ContextThemeWrapper
at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init> (MenuInflater.java:202)
at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:402)
at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:436)
at android.view.MenuInflater.parseMenu(MenuInflater.java:173)
at android.view.MenuInflater.parseMenu(MenuInflater.java:151)
at android.view.MenuInflater.inflate(MenuInflater.java:95)
at
it.mobile.activity.home.HomeActivity.onCreateOptionsMenu(HomeActivity.java:38)
at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:388)
at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2833)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: aboutDialog
[interface android.view.MenuItem]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getMethod(Class.java:915)
at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>
但我不明白问题出在哪里。与其他设备都工作正常!
But I don't understand where is the problem. With another device all works fine!
推荐答案
不知道如何与XML只是解决它。
从code的角度来看,你将不得不实施结果
公共布尔onOptionsItemSelected(菜单项项目)
然后根据不同的菜单项,调用所需的处理程序。
Not sure how to solve it with just xml.From code perspective you will have to implement
public boolean onOptionsItemSelected(MenuItem item)
and then depending on the menu item call the required handler.
这篇关于安卓的onClick不与Android 4.0.3工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!