ShareActionProvider from support v7 library doesn´t work

2.5k views Asked by At

I am trying to implement ShareActionProvider using support library from this tutorial http://developer.android.com/reference/android/support/v7/widget/ShareActionProvider.html Somehow it just doesn´t work as expected. Below is my code:

menu xml file:

<?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" >

<item 
    android:id="@+id/action_favorite"
    android:icon="@drawable/ic_favorite"
    myapp:showAsAction="always" />

<item 
    android:id="@+id/action_share"
    android:icon="@drawable/ic_share"
    myapp:showAsAction="always"
    myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

in the Activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.building_details_menu, menu);

    MenuItem item = (MenuItem) menu.findItem(R.id.action_share);
    ShareActionProvider shareAction = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

    Intent shareIntent = new Intent(Intent.ACTION_SEND)
        .putExtra(Intent.EXTRA_TEXT, "text")
        .setType("text/plain");
    shareAction.setShareIntent(shareIntent); //crashes here, shareAction is null

    return true;
}

I import android.support.v7.widget.ShareActionProvider in the Activity, I am sure there is no ClassCast exception like many people get. It is just null somehow. What do I do wrong?

1

There are 1 answers

1
Roman On BEST ANSWER

Happens that it is necessary for the CustomActivity to be child of ActionBarActivity. I changed this and the error disappeared.