更新我的项目以使用appcompat库到版本21.0.0后,我遇到了使用gridview multichoice模态事件创建的上下文菜单的问题。相同的代码适用于appcompat v20。
这是主要活动的相关部分:
public class MainActivity extends android.support.v7.app.ActionBarActivity
implements AbsListView.MultiChoiceModeListener {
...
mGridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
mGridView.setMultiChoiceModeListener(this);
@Override
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
mode.setTitle("Started");
mode.getMenuInflater().inflate(R.menu.context_menu, menu);
return true;
}
}
这是context_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_item_share"
android:title="Share..."
app:showAsAction="ifRoom"
android:icon="@android:drawable/ic_menu_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
这就是我回来的堆栈跟踪:
java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setActionProvider()
at android.support.v7.internal.view.menu.MenuItemImpl.setActionProvider(MenuItemImpl.java:628)
at android.support.v7.internal.view.menu.MenuItemWrapperICS.setSupportActionProvider(MenuItemWrapperICS.java:315)
at android.support.v4.view.MenuItemCompat.setActionProvider(MenuItemCompat.java:345)
at android.support.v7.internal.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:473)
at android.support.v7.internal.view.SupportMenuInflater$MenuState.addSubMenuItem(SupportMenuInflater.java:485)
at android.support.v7.internal.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:194)
at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:118)
at creativesdk.adobe.com.myapplication.MainActivity.onCreateActionMode(MainActivity.java:71)
at android.widget.AbsListView$MultiChoiceModeWrapper.onCreateActionMode(AbsListView.java:6165)
at android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onCreateActionMode(SupportActionModeWrapper.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase$ActionModeCallbackWrapper.onCreateActionMode(ActionBarActivityDelegateBase.java:1367)
at android.support.v7.internal.app.WindowDecorActionBar$ActionModeImpl.dispatchOnCreate(WindowDecorActionBar.java:1012)
at android.support.v7.internal.app.WindowDecorActionBar.startActionMode(WindowDecorActionBar.java:510)
at android.support.v7.app.ActionBarActivityDelegateBase.startSupportActionMode(ActionBarActivityDelegateBase.java:576)
at android.support.v7.app.ActionBarActivityDelegateHC.startActionModeForChild(ActionBarActivityDelegateHC.java:62)
at android.support.v7.internal.widget.NativeActionModeAwareLayout.startActionModeForChild(NativeActionModeAwareLayout.java:44)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:694)
at android.view.View.startActionMode(View.java:4857)
at android.widget.AbsListView.performLongPress(AbsListView.java:3102)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3061)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我很想知道是否有人发现同样的问题,以及是否有办法解决问题。
答案 0 :(得分:9)
尝试使用AppCompat v21从ActionMode充气菜单项时,ICS设备确实存在问题。似乎菜单项被包装了2次,并且调用了包装的项方法而不是本机方法,导致此异常。
Google需要在未来版本的AppCompat中修复此问题。
无论如何,这是我实施的一个hack,使其适用于当前版本:
1)在包android.support.v7.internal.view.menu
中创建一个实用程序类(必须使用此包以允许访问受包保护的方法而不使用反射):
package android.support.v7.internal.view.menu;
import android.view.Menu;
/**
* Hack to allow inflating ActionMode menus on Android 4.0.x with AppCompat v21
*/
public class MenuUnwrapper {
public static Menu unwrap(Menu menu) {
if (menu instanceof MenuWrapperICS) {
return ((MenuWrapperICS) menu).getWrappedObject();
}
return menu;
}
}
2)像这样给菜单充气:
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.context_menu, MenuUnwrapper.unwrap(menu));
return true;
}
编辑:
该错误已在AppCompat v21.0.2中修复,不再需要此hack。
更新您的工具。
答案 1 :(得分:4)
试试这个。它对我有用。
MenuItem menuItem = menu.findItem(R.id.search);
if (menuItem != null) {
MenuItemCompat.setOnActionExpandListener(menuItem,this);
MenuItemCompat.setActionView(menuItem, mSearchView);
}
答案 2 :(得分:3)
试试这个
inflater.inflate(R.menu.menu, menu);
// search
MenuItem item = menu.findItem(R.id.settings_search);
MenuItemCompat.setOnActionExpandListener(
item, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
答案 3 :(得分:0)
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Log.d(TAG, "onCreateActionMode");
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
MenuItemCompat.setActionProvider(item, mShareActionProvider);
return true;
}