问题描述
我试图在Android应用程序的操作栏共享按钮。在这里,我的code:
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v7.app.ActionBarActivity;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ShareActionProvider;
进口android.widget.TextView;
和片断部分:
{
私人字符串mForecastText;
公共PlaceholderFragment(){
setHasOptionsMenu(真正的);
}
私人意图sharedIntentMaker(){
意图shareIntent =新的意图(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType(text / plain的);
shareIntent.putExtra(Intent.EXTRA_TEXT,mForecastText +#SunshineApp);
返回shareIntent;
}
@覆盖
公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
inflater.inflate(R.menu.menu_detail,菜单);
菜单项菜单项= menu.findItem(R.id.menu_action_share);
ShareActionProvider mShareActionProvider =(ShareActionProvider)menuItem.getActionProvider();
mShareActionProvider.setShareIntent(sharedIntentMaker());
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_detail,集装箱,假);
TextView的textIntent =(TextView中)rootView.findViewById(R.id.textIntent);
意向意图= getActivity()getIntent()。
mForecastText = intent.getStringExtra(INT_PS);
textIntent.setText(mForecastText);
返回rootView;
}
当我跑我的模拟器,甚至真实设备的应用程序,我得到异常:
AndroidRuntime:致命异常:主要java.lang.UnsupportedOperationException:这是不支持,使用MenuItemCompat.getActionProvider()
和它链接到onCreateOptionsMenu()的字符串:
ShareActionProvider mShareActionProvider =(ShareActionProvider)menuItem.getActionProvider();
mShareActionProvider.setShareIntent(sharedIntentMaker());
我在做什么错了?
这是错误的logcat的P.S:堆栈跟踪:
java.lang.UnsupportedOperationException:这是不支持,使用MenuItemCompat.getActionProvider()
在android.support.v7.internal.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:645)
在com.project.malina.sunsine.DetailActivity$PlaceholderFragment.onCreateOptionsMenu(DetailActivity.java:70)
在android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1868)
在android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1989)
在android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:276)
在android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
在android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
在android.support.v7.app.ActionBarActivityDelegateBase.$p$pparePanel(ActionBarActivityDelegateBase.java:979)
在android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
在android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
在android.support.v7.app.ActionBarActivityDelegateBase $ 1.运行(ActionBarActivityDelegateBase.java:115)
在android.os.Handler.handleCallback(Handler.java:739)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5221)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
首先,你不能使用 android.widget.ShareActionProvider
与 appcompat- V7
行动起来吧反向移植(例如, ActionBarActivity
)。无论是使用 appcompat-V7
版本 ShareActionProvider
中,或将一切都交给本地操作栏。
第二,如果你坚持使用 appcompat-V7
,那么你就不能安全地使用 getActionProvider()
,因为这方法不会对API等级10及以下的存在。替换 menuItem.getActionProvider()
与 MenuItemCompat.getActionProvider(菜单项)
。
FWIW,这里是一个示例项目实现 appcompat-V7
版 ShareActionProvider
。
I'm trying to make "Share" button in Action Bar of Android Application.Here my code:
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;
and fragment part:
{
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;
}
When I run my app on emulator or even real device, I get exception:
AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()
and it links to onCreateOptionsMenu()'s strings:
ShareActionProvider mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
mShareActionProvider.setShareIntent(sharedIntentMaker());
What I'm doing wrong?
P.S.: Stacktrace of error from logcat:
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)
First, you cannot use android.widget.ShareActionProvider
with the appcompat-v7
action bar backport (e.g., ActionBarActivity
). Either use the appcompat-v7
version of ShareActionProvider
, or move everything over to the native action bar.
Second, if you stick with appcompat-v7
, then you cannot safely use getActionProvider()
, as that method will not exist on API Level 10 and below. Replace menuItem.getActionProvider()
with MenuItemCompat.getActionProvider(menuItem)
.
FWIW, here is a sample project that implements the appcompat-v7
edition of ShareActionProvider
.
这篇关于例外:这是不支持,使用MenuItemCompat.getActionProvider()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!