6

I try to use my action bar and i got this execption

   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)

I have read about this error and its something with my java paths.

I tried all kinds of solutins but i still got this error.

Here is my java path order and export:

enter image description here

My codes:

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

Where the exception is:

 @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);
        }

I imported

import android.support.v7.widget.ShareActionProvider;

Thank for helping

dasdasd
  • 1,971
  • 10
  • 45
  • 72
  • Can you show a screenshot of you're project structure? Is the the project imported into eclipse and referenced? I see you have a reference to the jar, but as far as I understand it should also be reference as a project. – QVDev Aug 08 '13 at 07:50
  • I added the reference to the project and its still not working – dasdasd Aug 08 '13 at 08:10
  • On what Android version do you get this exception? – mente Aug 08 '13 at 10:08
  • And after adding the project you still have the same error? – QVDev Aug 08 '13 at 13:06
  • similar to http://stackoverflow.com/questions/19118051/unable-to-cast-action-provider-to-share-action-provider – Paul Verest Oct 11 '14 at 09:18

4 Answers4

25

Add a new namespace 'myapp' or whatever like so

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto" >

Use that namespace for searchview and shareprovider.

see below

Change android:actionProviderClass="android.support.v7.widget.ShareActionProvider" to myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"

android:actionViewClass="android.support.v7.widget.SearchView" to myapp:actionViewClass="android.support.v7.widget.SearchView"

Also change namespace for showAsaction to myapp.

android:showAsAction="ifRoom|collapseActionView" to myapp:showAsAction="ifRoom|collapseActionView"

Hope that solves it. Check the doc here full info. ActionBar Documentation

DevBytes 6 Min Intro to ActionBar on YouTube

Thupten
  • 2,158
  • 1
  • 24
  • 31
2

try this from developer.android.com

 public boolean onCreateOptionsMenu(Menu menu) {
  // Get the menu item.
  MenuItem menuItem = menu.findItem(R.id.my_menu_item);
  // Get the provider and hold onto it to set/change the share intent.
  mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
  // Set history different from the default before getting the action
  // view since a call to MenuItemCompat.getActionView() calls
  // onCreateActionView() which uses the backing file name. Omit this
  // line if using the default share history file is desired.
  mShareActionProvider.setShareHistoryFileName("custom_share_history.xml");
  . . .

}

Federico Ponzi
  • 2,682
  • 4
  • 34
  • 60
TeeTracker
  • 7,064
  • 8
  • 40
  • 46
1

Suggestions.

First remove all support library.jar files from your libs folder in your main project. I.E. android-support-v4.jar...

Next navigate to your android sdk directory, and import the following library project. $SDK\extras\android\support\v7\appcompat.

After which fix any errors on the library project (may need to set android build target to latest one you have installed).

Now in your main project add this library project, by going to project properties in eclipse, clicking android, and add.

After which you may need to fix project imports, clean and build your project.

Danuofr
  • 1,661
  • 19
  • 25
0

Please make sure you are using the Searchview - android.support.v7.widget.SearchView and not the v4 library

DoronK
  • 4,837
  • 2
  • 32
  • 37