如何解决这个java.lang.UnsupportedOperationException:Android L不支持这个?

时间:2015-01-01 06:55:43

标签: java android android-actionbar toolbar

我尝试使用MultiChoiceModeListener多个项目点击共享项目我在onCreateActionMode方法中获得了FATAL EXCEPTION  我已经使用AppCompat v21.0.2,之后L更新此代码在AppCompat v19

中完美运行
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // TODO Auto-generated method stub
    mActionMode = mode;
    nr = 0;
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.contextual_menu, menu);

    MenuItem menuItem = menu.findItem(R.id.item_menu_share);
    mShareActionProvider = (ShareActionProvider) menuItem
        .getActionProvider();
    mShareActionProvider
        .setOnShareTargetSelectedListener(PlanetFragment.this);

    return true;
}

例外:

FATAL EXCEPTION: main
java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setActionProvider()
    at android.support.v7.internal.view.menu.MenuItemImpl.setActionProvider(MenuItemImpl.java:639)
    at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:445)
    at android.view.MenuInflater$MenuState.addSubMenuItem(MenuInflater.java:457)
    at android.view.MenuInflater.parseMenu(MenuInflater.java:186)
    at android.view.MenuInflater.inflate(MenuInflater.java:110)
    at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:109)
    at com.mydata.PlanetFragment$GetDataAsyncTask$1.onCreateActionMode(PlanetFragment.java:252)
    at android.widget.AbsListView$MultiChoiceModeWrapper.onCreateActionMode(AbsListView.java:6203)
    at android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onCreateActionMode(SupportActionModeWrapper.java:154)
    at android.support.v7.app.ActionBarActivityDelegateBase$ActionModeCallbackWrapper.onCreateActionMode(ActionBarActivityDelegateBase.java:1297)
    at android.support.v7.app.ActionBarActivityDelegateBase.startSupportActionModeFromWindow(ActionBarActivityDelegateBase.java:648)
    at android.support.v7.app.ActionBarActivityDelegate$1.startActionMode(ActionBarActivityDelegate.java:99)
    at android.support.v7.widget.WindowCallbackWrapper.startActionMode(WindowCallbackWrapper.java:69)
    at android.support.v7.internal.app.ToolbarActionBar.startActionMode(ToolbarActionBar.java:206)
    at android.support.v7.app.ActionBarActivityDelegateBase.startSupportActionMode(ActionBarActivityDelegateBase.java:581)
    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:677)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:677)
    at android.view.View.startActionMode(View.java:4347)
    at android.widget.AbsListView.performLongPress(AbsListView.java:2874)
    at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2834)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5162)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
    at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

这是一个老问题,但我会发布我的解决方案,也许有人会发现它可用。

@Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            Log.d(TAG, "onCreateActionMode");

            mode.getMenuInflater().inflate(R.menu.list_select_menu, menu);
            MenuItem item = menu.findItem(R.id.menu_item_share);
            mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
            MenuItemCompat.setActionProvider(item, mShareActionProvider);

            return true;
        }

答案 1 :(得分:0)

变化:

mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();

mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);