Bruce
Bruce

Reputation: 2584

getActionProvider is undefined for the type MenuItemCompat?

I'm trying to add a sharing provider to my action bar following the guide here: http://developer.android.com/guide/topics/ui/actionbar.html#ShareActionProvider

However when trying to add the below as instructed inside my onCreateOptionsMenu()

// Set up ShareActionProvider's default share intent
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider)
        MenuItemCompat.getActionProvider(shareItem);
mShareActionProvider.setShareIntent(getDefaultIntent());

There's an error saying that getActionProvider is undefined for the type MenuItemCompat, though it is documented: http://developer.android.com/reference/android/support/v4/view/MenuItemCompat.html#getActionProvider(android.view.MenuItem)

Any ideas?

Thanks!

Upvotes: 1

Views: 1197

Answers (1)

iamreptar
iamreptar

Reputation: 1451

Without seeing your XML, define your actionProviderClass in your menu layout:

<item android:id=...
...
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

and obtain a reference to the ShareActionProvider with

ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

Keep in mind, the documentation uses examples from the support library. If this is the case then, in your menu layout:

<item android:id=...
...
yourapp:actionProviderClass="android.widget.ShareActionProvider"/>

and to obtain a reference to the ShareActionProvider:

ShareActionProvider shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();

Upvotes: 7

Related Questions