Gjhuizing Gjhuizing - 1 month ago 10
Android Question

Cannot cast from ActionProvider to ShareActionProvider

I am trying to implement a ShareActionProvider in my ActionBarSherlock.

The following code gives me this error :


Cannot cast from ActionProvider to ShareActionProvider


ReadingActivity.java

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu2b, (Menu) menu);
menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.getItem(1).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);

MenuItem menuItem = menu.findItem(R.id.share);
// Get the provider and hold onto it to set/change the share intent.
ShareActionProvider shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();

// Set the default share intent
shareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}


menu2b.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
...
<item android:id="@+id/share"
android:title="Share"
android:icon="@drawable/ic_share_action_bar"
android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>

Answer

You should be using the ShareActionProvider from ABS, not the stock one.

android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
Comments