ClassCastException: android.support.v7.widget.ShareActionProvider

I try to use my action bar and I got this execution

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 read about this error and its something with my java tracks.

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

Here is my order and export of java route:

enter image description here

My codes are:

 <?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 is the exception:

  @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 have imported

 import android.support.v7.widget.ShareActionProvider; 

thanks for the help

+6
source share
4 answers

Add a new namespace "myapp" or something like this

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

Use this namespace for search 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 the namespace for showAsaction for myapp.

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

Hope this solves. Check all documentation. ActionBar Documentation

DevBytes 6 Min Intro to ActionBar on YouTube

+25
source

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

}

+2
source

Suggestions.

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

Then go to the android sdk directory and import the following library project. $ SDK \ Extras \ Android \ support \ v7 \ AppCompat.

Then fix any errors in the library project (perhaps you need to set the goal of the Android build for the latter that you installed).

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

After that, you may need to fix the import of the project, clean and build the project.

+1
source

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

0
source

Source: https://habr.com/ru/post/951275/


All Articles