支持v7库的ShareActionProvider不起作用 [英] ShareActionProvider from support v7 library doesn´t work

查看:186
本文介绍了支持v7库的ShareActionProvider不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用本教程 http://developer.android.com/reference/android/support/v7/widget/ShareActionProvider.html 不知何故,它无法正常工作.下面是我的代码:

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:

菜单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" >

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

在活动中:

@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;
}

我在Activity中导入了android.support.v7.widget.ShareActionProvider,我确信没有像很多人一样的ClassCast异常.它只是以某种方式为空.我该怎么办?

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?

推荐答案

可能是CustomActivity必须是ActionBarActivity的子级.我对此进行了更改,错误消失了.

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

这篇关于支持v7库的ShareActionProvider不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆