Unable to cast Action Provider to Share Action Provider

Solution 1:

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

1) You have to use:

<menu xmlns:android="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"/>
</menu>

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:

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:

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.