首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >异常:这不受支持,请使用MenuItemCompat.getActionProvider()

异常:这不受支持,请使用MenuItemCompat.getActionProvider()
EN

Stack Overflow用户
提问于 2015-01-11 21:38:13
回答 3查看 17.4K关注 0票数 18

我正在尝试在Android应用程序的操作栏中创建“分享”按钮。下面是我的代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ShareActionProvider;
import android.widget.TextView;

和片段部分:

{
private String mForecastText;

public PlaceholderFragment() {
    setHasOptionsMenu(true);
}

private Intent sharedIntentMaker(){
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, mForecastText + "#SunshineApp");
    return shareIntent;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_detail, menu);
    MenuItem menuItem = menu.findItem(R.id.menu_action_share);
    ShareActionProvider mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
    mShareActionProvider.setShareIntent(sharedIntentMaker());

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_detail, container, false);

    TextView textIntent = (TextView) rootView.findViewById(R.id.textIntent);
    Intent intent = getActivity().getIntent();
    mForecastText = intent.getStringExtra("INT_PS");
    textIntent.setText(mForecastText);
    return rootView;
}

当我在模拟器甚至真实设备上运行我的应用程序时,我得到了异常:

AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()

并且它链接到onCreateOptionsMenu()的字符串:

ShareActionProvider mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
mShareActionProvider.setShareIntent(sharedIntentMaker());

我哪里做错了?

附注:来自logcat的错误堆栈跟踪:

01-11 13:03:17.490 2331 -2331/com.*E/AndroidRuntime:致命异常:主进程:com.*,PID: 2331

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()

        at android.support.v7.internal.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:645)
        at com.project.malina.sunsine.DetailActivity$PlaceholderFragment.onCreateOptionsMenu(DetailActivity.java:70)
        at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1868)
        at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1989)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:276)
        at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
        at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
        at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
        at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
        at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
        at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:115)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-01-11 21:44:27

首先,您不能将android.widget.ShareActionProviderappcompat-v7操作栏backport (例如,ActionBarActivity)一起使用。要么使用ShareActionProviderappcompat-v7版本,要么将所有内容移动到本机操作栏。

其次,如果您坚持使用appcompat-v7,那么您就不能安全地使用getActionProvider(),因为该方法在API10及更低级别上将不存在。用MenuItemCompat.getActionProvider(menuItem)替换menuItem.getActionProvider()

FWIW,实现ShareActionProviderappcompat-v7版本的here is a sample project

票数 68
EN

Stack Overflow用户

发布于 2015-10-09 08:21:22

您可以在下面的链接中遵循Google的Code Sample中的模式。https://github.com/googlesamples/android-ActionBarCompat-ShareActionProvider

最简单的方法是转到Android Studio =>文件,导入示例。然后键入“共享操作提供程序”。

下面是使用ActionBarCompat的ShareActionProvider创建共享操作菜单项的代码,向后兼容v7接口。

MainActivity.java

// BEGIN_INCLUDE(get_sap)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu resource
    getMenuInflater().inflate(R.menu.main_menu, menu);

    // Retrieve the share menu item
    MenuItem shareItem = menu.findItem(R.id.menu_share);

    // Now get the ShareActionProvider from the item
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

    // Get the ViewPager's current item position and set its ShareIntent.
    int currentViewPagerItem = ((ViewPager) findViewById(R.id.viewpager)).getCurrentItem();
    setShareIntent(currentViewPagerItem);

    return super.onCreateOptionsMenu(menu);
}
// END_INCLUDE(get_sap

private void setShareIntent(int position) {
    // BEGIN_INCLUDE(update_sap)
    if (mShareActionProvider != null) {
        // Get the currently selected item, and retrieve it's share intent
        ContentItem item = mItems.get(position);
        Intent shareIntent = item.getShareIntent(MainActivity.this);

        // Now update the ShareActionProvider with the new share intent
        mShareActionProvider.setShareIntent(shareIntent);
    }
    // END_INCLUDE(update_sap)
}

main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:support="http://schemas.android.com/apk/res-auto">

<!--
  To use ShareActionProvider provided by ActionBarCompat, we reference the class by set the
  support:actionProviderClass attribute with the full class name of ShareActionProvider.
-->
<item
    android:id="@+id/menu_share"
    android:title="@string/menu_share"
    support:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    support:showAsAction="always" />

票数 11
EN

Stack Overflow用户

发布于 2018-08-22 21:05:01

请执行以下步骤:

步骤1)要在活动中添加“共享”操作,请在应用程序栏的菜单资源中放置一个ShareActionProvider。像这样

<item android:id="@+id/action_share"
      android:title="@string/share"
      app:showAsAction="ifRoom"
      app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

请注意actionProviderClass

步骤2)现在,您要确保在活动Java Class中导入相同的ShareActionProvider

import android.support.v7.widget.ShareActionProvider;

步骤3)

下面是onCreate方法的部分

        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.main_activity_bar, menu);

        ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menu.findItem(R.id.actionbar_share));
        shareActionProvider.setShareIntent(shareIt());

意图方法

    private Intent shareIt() {
        Intent intent= new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");

        String shareMsgBody = "Hello, Share this with world !!";

        intent.putExtra(Intent.EXTRA_TEXT, shareAndPromotionBody);
        startActivity(Intent.createChooser(intent, "Spread the message!!"));
        return intent;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27887716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档