Unable to cast Action Provider to Share Action Provider

AndroidShareactionprovider

Android Problem Overview


Below is the code for my Activity

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v7.widget.ShareActionProvider;
    import android.view.Menu;
    import android.view.MenuItem;

    public class MainActivity extends Activity {
	private ShareActionProvider shareAction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		MenuItem item = menu.getItem(R.id.menu_settings);
		shareAction = (ShareActionProvider) item.getActionProvider();
		return true;
	}
}

The Problem is that there is a cast error from ActionProvider to ShareActionProvider.Why it is so below is activity_menu.xml

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

<item
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/menu_settings"
    android:actionProviderClass="android.widget.ShareActionProvider"
    />

</menu>

Android Solutions


Solution 1 - Android

I had the same problem and I have found the solution:

  1. You have to use:

    http://schemas.android.com/apk/res/android" xmlns:bwq="http://schemas.android.com/apk/res-auto" >

     <item
         android:id="@+id/menu_share"
         android:title="@string/menu_share"
         bwq:actionProviderClass="android.support.v7.widget.ShareActionProvider"
         bwq:showAsAction="always"/>
    

  2. and in Java

    import android.support.v7.widget.ShareActionProvider;

and

// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

Solution 2 - Android

menu:

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

java:

MenuItem menuItem = menu.findItem(R.id.action_share);
mActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

Solution 3 - Android

You are using android.widget.ShareActionProvider, which is for the native API Level 11+ action bar. If you are using the AppCompat backport of the action bar, you need to use android.support.v7.widget.ShareActionProvider instead.

Solution 4 - Android

I ran into this problem following the android dev actionbar guide which is basically what you are doing. After digging into the samples that utilize the action bar using the backwards compatible v7 and v4 support libraries I ended up using the following code for onCreateOptionsMenu().

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        File file = new File(mFilePath);
       
       
        ShareCompat.IntentBuilder b = ShareCompat.IntentBuilder.from(this)
        .setType("image/png")
        .setStream(Uri.fromFile(file));
       
      
        MenuItem item = menu.add("Share");
        ShareCompat.configureMenuItem(item, b);
        MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }

Couple of things to note here is that you are not inflating from a menu resource. The menu is having the default share button added to it. You simply need to specify what type of resource your are sharing with .setType. Since I am sharing a file I need to setStream, with Uri.fromFile(new File()); If you were sharing text you would setType("text/plain").

Also make sure you have imported the $SDK\extras\android\support\v7\appcompat library project, which contains the needed packages. Also since have imported that library project, your project does not need the v4support.jar in your libs folder because the library project already has it.

Solution 5 - Android

This was asked years ago, so the existing answers probably worked back then. However, at the time of this writing the suggested code gives many deprecation warnings and it doesn't solve the problem.

I did eventually solve the problem and it was not documented anywhere on the web (that I could find), so hopefully this answer will help people who currently run into this same problem.

The solution for me was in the import statement. When I used SharedActionProvider for the first time, Android Studio can add the import automatically. It provides two options for what to import: android.widget.ShareActionProvider and androidx.appcompat.widget.ShareActionProvider.

The former is broken and results in the error about the cast never succeeding. The latter will make everything work properly. The app:ActionProviderClass in the menu file must be identical to the imported file name.

Solution 6 - Android

The problem was that what @CommonsWare said about not using the support library ShareActionProvider and also even if i did then it also wont have worked out because when using Support Library we require Custom Prefixes for some actions like showAsAction

Solution 7 - Android

Follow a Simple rule that I found useful

With AppCompatActivity use this,

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

    <!--
      To use ShareActionProvider, we reference the class by set the
      support:actionProviderClass attribute with the full class name of ShareActionProvider.
    -->
    <item
        android:id="@+id/menu_share"
        android:title="@string/menu_share"
        support:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        support:showAsAction="always" />

</menu>

You can also replace support:actionProviderClass with app:actionProviderClass and support:showAsAction with app:showAsAction

In your onCreateOptionsMenu()

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu resource
        getMenuInflater().inflate(R.menu.main_menu, menu);

        // Retrieve the share menu item
        MenuItem shareItem = menu.findItem(R.id.menu_share);

        // Now get the ShareActionProvider from the item
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

         //set its ShareIntent.
        setShareIntent(shareIntent);

        return super.onCreateOptionsMenu(menu);
    }

With Activity use this,

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

	<item
		android:id="@+id/share"
		android:actionProviderClass="android.widget.ShareActionProvider"
		android:showAsAction="ifRoom"
		tools:ignore="MenuTitle" />

</menu>

In your onCreateOptionsMenu()

@Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actions, menu);

    ShareActionProvider share=
        (ShareActionProvider)menu.findItem(R.id.share)
                                 .getActionProvider();
    share.setShareIntent(shareIntent);

    return(super.onCreateOptionsMenu(menu));
  }

Solution 8 - Android

None of the solutions here solved my problem with ShareActionProvider, not casting / returning null. I ended up replacing ShareActionProvider with just an Intent.SEND_ACTION to share images in my app, like presented in Android Developers tutorial: https://developer.android.com/training/sharing/send

Although Google mentions in this tutorial that: Note: The best way to add a share action item to an ActionBar is to use ShareActionProvider, which became available in API level 14. ShareActionProvider is discussed in the lesson about Adding an Easy Share Action. I found much simpler to implement just the Intention.SEND_ACTION. Not sure if there are other reasons to implement the ShareActionProvider...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnuranjit MaindolaView Question on Stackoverflow
Solution 1 - AndroidSusana Mar FloresView Answer on Stackoverflow
Solution 2 - AndroidkostbashView Answer on Stackoverflow
Solution 3 - AndroidCommonsWareView Answer on Stackoverflow
Solution 4 - AndroidDanuofrView Answer on Stackoverflow
Solution 5 - AndroidBas WijnenView Answer on Stackoverflow
Solution 6 - AndroidAnuranjit MaindolaView Answer on Stackoverflow
Solution 7 - AndroidShubhamhackzView Answer on Stackoverflow
Solution 8 - Androidofri cofriView Answer on Stackoverflow