3

我正在尝试实现 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

菜单.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)
4

3 回答 3

5

改成

<?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

于 2014-02-20T09:30:36.597 回答
0

它对我有用。Git Hub 链接

添加这个你的java类:

import android.widget.ShareActionProvider;
......
private ShareActionProvider m_provider;
......
{
  m_provider = (ShareActionProvider)menu_item.getActionProvider();
}

将此添加到您的menu.xml

android:actionProviderClass="android.widget.ShareActionProvider"
于 2015-05-06T10:41:36.783 回答
0

我今天在Android Developer Guide上添加 Easy Share Action时遇到了这个问题。


事实证明,在 Android 4.0 之前的平台上,SharedActionProvider 需要与 v7 兼容支持库android.support.v7.widget.ShareActionProvider一起使用,这意味着您需要:

import android.support.v7.widget.ShareActionProvider;

在您的活动 java 文件中。

但实际上在Android 4.0之后,ShareActionProvider android.widget.ShareActionProvider就可以使用了。因此导入应该是:

import android.widget.ShareActionProvider;



您的问题是:在menu.xml中您使用了 android.support.v7.widget.ShareActionProvider,但在活动 java 文件中您导入了 android.support.v7.widget.ShareActionProvider。因此,只需将它们更改为相同的就可以解决问题,android.support.v7.widget.ShareActionProvider(如果您的 minSDK < 14)或 android.widget.ShareActionProvider(如果您的 minSDK >= 14)都可以。

于 2016-12-03T11:58:06.833 回答