0

我尝试使用我的操作栏,我得到了这个执行

   08-08 00:54:08.913: E/AndroidRuntime(18468): FATAL EXCEPTION: main
08-08 00:54:08.913: E/AndroidRuntime(18468): java.lang.ClassCastException: android.support.v7.widget.ShareActionProvider cannot be cast to android.view.ActionProvider
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.view.MenuInflater$MenuState.readItem(MenuInflater.java:374)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.view.MenuInflater.parseMenu(MenuInflater.java:160)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.view.MenuInflater.inflate(MenuInflater.java:110)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at com.example.workoutlog.AddWorkOutPage.onCreateOptionsMenu(AddWorkOutPage.java:3190)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:460)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:822)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:253)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.view.Choreographer.doFrame(Choreographer.java:531)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.os.Handler.handleCallback(Handler.java:725)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.os.Looper.loop(Looper.java:137)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at android.app.ActivityThread.main(ActivityThread.java:5226)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at java.lang.reflect.Method.invokeNative(Native Method)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at java.lang.reflect.Method.invoke(Method.java:511)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-08 00:54:08.913: E/AndroidRuntime(18468):    at dalvik.system.NativeStart.main(Native Method)

我已经阅读了有关此错误及其与我的 java 路径有关的内容。

我尝试了各种解决方案,但仍然出现此错误。

这是我的 java 路径顺序和导出:

在此处输入图像描述

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/save_wotkout_ab"
    android:title="Add Item"
    android:icon="@android:drawable/ic_menu_save"
    android:showAsAction="ifRoom"/> 

<item android:id="@+id/delete_workout_ab"
    android:title="Add Item"
    android:icon="@android:drawable/ic_delete"
    android:showAsAction="ifRoom"/> 

<item android:id="@+id/search_ab"
    android:title="Add Item"
    android:icon="@android:drawable/ic_menu_search"
    android:actionViewClass="android.support.v7.widget.SearchView"
    android:showAsAction="ifRoom|collapseActionView"/> 

    <item
    android:id="@+id/action_share"
    android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    android:showAsAction="ifRoom"
    android:title="share"/>

</menu>

例外情况是:

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu items for use in the action bar
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.add_workout_actionbar, menu);

            MenuItem shareItem = menu.findItem(R.id.action_share);
            mShareActionProvider = (ShareActionProvider)
                    MenuItemCompat.getActionProvider(shareItem);
            mShareActionProvider.setShareIntent(getDefaultIntent());

            return super.onCreateOptionsMenu(menu);
        }

感谢您的帮助

4

4 回答 4

1

Android 设计添加操作提供程序

于 2013-09-08T14:21:40.420 回答
1

您需要定义一个自定义命名空间并将该命名空间用于showAsActionActionProviderClass属性,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlsns:myapp="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/save_wotkout_ab"
    android:title="Add Item"
    android:icon="@android:drawable/ic_menu_save"
    android:showAsAction="ifRoom"/> 

<item android:id="@+id/delete_workout_ab"
    android:title="Add Item"
    android:icon="@android:drawable/ic_delete"
    android:showAsAction="ifRoom"/> 

<item android:id="@+id/search_ab"
    android:title="Add Item"
    android:icon="@android:drawable/ic_menu_search"
    android:actionViewClass="android.support.v7.widget.SearchView"
    android:showAsAction="ifRoom|collapseActionView"/> 

    <item
    android:id="@+id/action_share"
    myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    myapp:showAsAction="ifRoom"
    android:title="share"/>

</menu>
于 2014-06-16T15:11:07.410 回答
0

请确保您使用的是 Searchview - android.support.v7.widget.SearchView 而不是 v4 库

于 2013-09-08T14:17:03.480 回答
0
java.lang.ClassCastException: android.support.v7.widget.ShareActionProvider cannot be cast to android.view.ActionProvider

您决定不提供任何代码,因此很难为您提供太多帮助。但是,在 的第 3190 行AddWorkOutPage.java,您正试图将 anandroid.support.v7.widget.ShareActionProvider转换为android.view.ActionProvider. 那不管用。我的猜测是您选择了错误的导入,并且已经导入android.view.ActionProvider而不是android.support.v7.widget.ActionProvider.

于 2013-08-07T22:17:26.083 回答