android – appcompat v21 throws java.lang.UnsupportedOperationException

前端之家收集整理的这篇文章主要介绍了android – appcompat v21 throws java.lang.UnsupportedOperationException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在更新我的项目以使用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)

我很好奇,知道有没有人发现同样的问题,如果有办法.

解决方法

当使用AppCompat v21从ActionMode中增加菜单项时,ICS设备确实存在问题.看起来菜单项被包装了2次,一个被包装的item方法调用而不是原生的方法,导致这个异常.

Google需要在将来版本的AppCompat中解决此问题.

无论如何,这是一个黑客,我实现了使它与当前版本的工作:

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中得到修复,并且此黑客不再需要.

更新你的工具

猜你在找的Android相关文章