Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException: android.support.v7.widget.ShareActionProvider

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

like image 261
dasdasd Avatar asked Aug 08 '13 07:08

dasdasd


3 Answers

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

like image 57
Thupten Avatar answered Oct 19 '22 11:10

Thupten


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");
  . . .

}

like image 2
TeeTracker Avatar answered Oct 19 '22 11:10

TeeTracker


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.

like image 1
Danuofr Avatar answered Oct 19 '22 09:10

Danuofr