why menuitemcompat getactionprovider returns null using -'java,android'

why menuitemcompat getactionprovider returns null  using -'java,android'

I tried to use android.support.v7.widget.ShareActionProvider on actionbar in my app. So I followed the example from android document but got some issues.
Here's my menu xml:

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

        myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />


here's my code to create the share action button:

public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.share, menu);
    MenuItem shareItem = menu.findItem(R.id.action_share);
    ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);
    super.onCreateOptionsMenu(menu, inflater);

My question is:

MenuItemCompat.getActionProvider(shareItem) always returns null for me, why's that?
When I comment those lines, the share button appears on the bar but does nothing while clicking, how to fix it(if question 1 can't be solved)?

btw, I checked codes of MenuItemCompat.getActionProvider, it looks like this method will check if the menu item implements SupportMenuItem interface and returns fail if it isn't. How could I deal with it?

asked Sep 7, 2015 by rajesh
0 votes

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.