Android 为什么ActionBarSherlock没有响应?
我是安卓的不速之客,我使用ActionBarSherlock的菜单栏访问菜单。在低于API11的android API上,一切正常,但对于任何API11及以上的API,菜单栏/菜单项都没有响应。当我单击菜单项时,它们会高亮显示,但不会执行。这几乎就像是菜单项丢失了它们的侦听器。是否有我忘记实现的设置?非常感谢您的帮助 我的代码:Android 为什么ActionBarSherlock没有响应?,android,menu,actionbarsherlock,listener,Android,Menu,Actionbarsherlock,Listener,我是安卓的不速之客,我使用ActionBarSherlock的菜单栏访问菜单。在低于API11的android API上,一切正常,但对于任何API11及以上的API,菜单栏/菜单项都没有响应。当我单击菜单项时,它们会高亮显示,但不会执行。这几乎就像是菜单项丢失了它们的侦听器。是否有我忘记实现的设置?非常感谢您的帮助 我的代码: //My Sherlock wrapper ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);
//My Sherlock wrapper
ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);
//OnCreate
setTheme(R.style.Theme_Sherlock);
mSherlock.setContentView(R.layout.main);
//Menu Methods
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case 1: // id from the xml file
Intent i = new Intent("com.bmoney.GSCC.OPTIONS");
startActivity(i);
return true; // we handled the click, dont pass it up the chain
case 2: // id from the xml file
Intent i2 = new Intent("com.bmoney.GSCC.PREFS");
startActivity(i2);
return true;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
return mSherlock.dispatchCreateOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { //<-- has Sherlock Menu Import
menu.add(0,1,0,"Preferences").setIcon(R.drawable.ic_action_example).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,2,0,"Help").setIcon(R.drawable.info).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
//我的夏洛克包装器
ActionBarSherlock mSherlock=ActionBarSherlock.wrap(此);
//一次创建
setTheme(R.style.Theme_Sherlock);
mSherlock.setContentView(R.layout.main);
//菜单方法
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//TODO自动生成的方法存根
开关(item.getItemId())
{
案例1://来自xml文件的id
意向i=新意向(“com.bmoney.GSCC.OPTIONS”);
星触觉(i);
return true;//我们处理了单击,不要将其传递到链上
案例2://来自xml文件的id
意向i2=新意向(“com.bmoney.GSCC.PREFS”);
星触觉(i2);
返回true;
}
返回false;
}
@凌驾
公共布尔onCreateOptions菜单(android.view.Menu菜单){
//TODO自动生成的方法存根
返回mSherlock.DispatchCreateOptions菜单(菜单);
}
@凌驾
public boolean oncreateoptions Menu(Menu Menu){/如果我不得不猜测的话,您对MenuItem
的导入是针对android.view.MenuItem
,而不是Sherlock的等效项
如果是,我建议:
- 将
@Override
添加到onOptionsItemSelected()
- 您删除所有的
android.view.*
导入,然后将它们重新添加为Sherlock导入(例如,通过Eclipse中的Ctrl-Shift-O)
- 您可以使用带有Sherlock导入的方法合并两个
onCreateOptionsMenu()
方法
我认为答案是,在处理菜单事件时需要“返回true”
此外,您可能会发现,如果您将方法重组为以下内容,那么您将更容易阅读和维护它
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.options: // id from the xml file
Intent i = new Intent("com.bmoney.GSCC.OPTIONS");
startActivity(i);
return true; // we handled the click, dont pass it up the chain
case R.id.prefs: // id from the xml file
Intent i = new Intent("com.bmoney.GSCC.PREFS");
startActivity(i);
return true;
}
return false;
}我认为在OnCreateOptions菜单中添加菜单项时,应该在菜单项中添加OnMenuItemClickListener。然后添加OnMenuItemSelected方法并实现onOptionItemSelected方法中onOptionItemSelected中的代码。所以你应该
@Override
public boolean onMenuItemClick(MenuItem item) {
// Code from inside onoptionItemSelected
}
谢谢你的回复。我的menuitem Import已经和夏洛克一样了。我遵循了您的建议,但是当我整合OnCreateOptions菜单时,菜单项不会显示在菜单栏中。请检查我的编辑。我做错什么了吗?@B.Money:除了科里·斯科特的回答,我什么也没想到。下面是一个带有工作操作栏的示例应用程序: