ShareActionProvider from support v7 library doesn´t work

China☆狼群 提交于 2019-12-22 09:21:38

问题


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:


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



来源:https://stackoverflow.com/questions/20873875/shareactionprovider-from-support-v7-library-doesn%c2%b4t-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!