ClassCastException:android.support.v7.widget

时间:2013-08-07 21:59:03

标签: android android-actionbar

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

   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路径顺序和导出:

enter image description here

我的代码:

<?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 个答案:

答案 0 :(得分:1)

Android Design Adding a Action Provider

答案 1 :(得分: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>

答案 2 :(得分:0)

java.lang.ClassCastException: android.support.v7.widget.ShareActionProvider cannot be cast to android.view.ActionProvider

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

答案 3 :(得分:0)

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