Unable to cast Action Provider to Share Action Provider

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

Leave a Comment