ClassCastException:android.support.v7.widget.ShareActionProvider
作者:互联网
我正在尝试实现ShareActionProvider,但得到:
02-20 04:14:28.894: E/AndroidRuntime(1145): Process: com.learn2crack.tab, PID: 1145
02-20 04:14:28.894: E/AndroidRuntime(1145): java.lang.ClassCastException: android.support.v7.widget.ShareActionProvider cannot be cast to android.view.ActionProvider
menu.xml文件:
<item
android:id="@+id/share"
android:title="@string/share"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
android:showAsAction="always" />
我把它导入了我的班级:
import android.support.v7.widget.ShareActionProvider;
完整日志:
02-20 04:14:28.894: E/AndroidRuntime(1145): FATAL EXCEPTION: main
02-20 04:14:28.894: E/AndroidRuntime(1145): Process: com.learn2crack.tab, PID: 1145
02-20 04:14:28.894: E/AndroidRuntime(1145): java.lang.ClassCastException: android.support.v7.widget.ShareActionProvider cannot be cast to android.view.ActionProvider
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.view.MenuInflater$MenuState.readItem(MenuInflater.java:374)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.view.MenuInflater.parseMenu(MenuInflater.java:160)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.view.MenuInflater.inflate(MenuInflater.java:110)
02-20 04:14:28.894: E/AndroidRuntime(1145): at com.learn2crack.tab.MainActivity.onCreateOptionsMenu(MainActivity.java:72)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
02-20 04:14:28.894: E/AndroidRuntime(1145): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
02-20 04:14:28.894: E/AndroidRuntime(1145): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
02-20 04:14:28.894: E/AndroidRuntime(1145): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.view.Choreographer.doFrame(Choreographer.java:543)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.os.Handler.handleCallback(Handler.java:733)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.os.Looper.loop(Looper.java:136)
02-20 04:14:28.894: E/AndroidRuntime(1145): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-20 04:14:28.894: E/AndroidRuntime(1145): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 04:14:28.894: E/AndroidRuntime(1145): at java.lang.reflect.Method.invoke(Method.java:515)
02-20 04:14:28.894: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-20 04:14:28.894: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-20 04:14:28.894: E/AndroidRuntime(1145): at dalvik.system.NativeStart.main(Native Method)
解决方法:
改成
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/share"
android:title="@string/share"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
yourapp:showAsAction="always" />
...
</menu>
检查主题使用ShareActionProvider
http://developer.android.com/guide/topics/ui/actionbar.html
标签:android,shareactionprovider 来源: https://codeday.me/bug/20190831/1772636.html