android - appcompat v21 throws java.lang.UnsupportedOperationException -
android - appcompat v21 throws java.lang.UnsupportedOperationException -
after updating project utilize appcompat library version 21.0.0 have problem context menu created gridview multichoice modal event. same code works nice appcompat v20.
this relevant part of main activity:
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); homecoming true; } }
and 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>
and stacktrace i'm getting back:
java.lang.unsupportedoperationexception: not supported, utilize menuitemcompat.setactionprovider() @ android.support.v7.internal.view.menu.menuitemimpl.setactionprovider(menuitemimpl.java:628) @ android.support.v7.internal.view.menu.menuitemwrapperics.setsupportactionprovider(menuitemwrapperics.java:315) @ android.support.v4.view.menuitemcompat.setactionprovider(menuitemcompat.java:345) @ android.support.v7.internal.view.supportmenuinflater$menustate.setitem(supportmenuinflater.java:473) @ android.support.v7.internal.view.supportmenuinflater$menustate.addsubmenuitem(supportmenuinflater.java:485) @ android.support.v7.internal.view.supportmenuinflater.parsemenu(supportmenuinflater.java:194) @ android.support.v7.internal.view.supportmenuinflater.inflate(supportmenuinflater.java:118) @ creativesdk.adobe.com.myapplication.mainactivity.oncreateactionmode(mainactivity.java:71) @ android.widget.abslistview$multichoicemodewrapper.oncreateactionmode(abslistview.java:6165) @ android.support.v7.internal.view.supportactionmodewrapper$callbackwrapper.oncreateactionmode(supportactionmodewrapper.java:151) @ android.support.v7.app.actionbaractivitydelegatebase$actionmodecallbackwrapper.oncreateactionmode(actionbaractivitydelegatebase.java:1367) @ android.support.v7.internal.app.windowdecoractionbar$actionmodeimpl.dispatchoncreate(windowdecoractionbar.java:1012) @ android.support.v7.internal.app.windowdecoractionbar.startactionmode(windowdecoractionbar.java:510) @ android.support.v7.app.actionbaractivitydelegatebase.startsupportactionmode(actionbaractivitydelegatebase.java:576) @ android.support.v7.app.actionbaractivitydelegatehc.startactionmodeforchild(actionbaractivitydelegatehc.java:62) @ android.support.v7.internal.widget.nativeactionmodeawarelayout.startactionmodeforchild(nativeactionmodeawarelayout.java:44) @ android.view.viewgroup.startactionmodeforchild(viewgroup.java:694) @ android.view.view.startactionmode(view.java:4857) @ android.widget.abslistview.performlongpress(abslistview.java:3102) @ android.widget.abslistview$checkforlongpress.run(abslistview.java:3061) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
i'm curious know if found same problem , if there's way around it.
there indeed issue on ics devices when trying inflate menu item actionmode appcompat v21. seems menu items wrapped 2 times , wrapped item method gets called instead of native one, causing exception.
google needs prepare in future version of appcompat.
anyway, here's hack implemented create work current release:
1) create utility class in bundle android.support.v7.internal.view.menu
(using bundle mandatory allow accessing package-protected methods without using reflection):
package android.support.v7.internal.view.menu; import android.view.menu; /** * hack allow inflating actionmode menus on android 4.0.x appcompat v21 */ public class menuunwrapper { public static menu unwrap(menu menu) { if (menu instanceof menuwrapperics) { homecoming ((menuwrapperics) menu).getwrappedobject(); } homecoming menu; } }
2) inflate menu this:
@override public boolean oncreateactionmode(actionmode mode, menu menu) { mode.getmenuinflater().inflate(r.menu.context_menu, menuunwrapper.unwrap(menu)); homecoming true; }
edit:
the bug has been fixed in appcompat v21.0.2 , hack no longer necessary.
update tools.
android android-support-library android-actionbar-compat contextual-action-bar
Comments
Post a Comment