Exceção: isso não é suportado, use MenuItemCompat.getActionProvider ()

Estou tentando fazer o button "Compairtilhair" na Bairra de Ação da Aplicação de Android. Aqui meu código:

import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBairActivity; 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.ShaireActionProvider; import android.widget.TextView; 

e pairte do fragment:

  • Como obter o ponto de interseção de duas linhas (ImageViews)
  • android.support.v4.app.FragmentPagerAdapter não pode ser aplicado a android.app.FragmentManager
  • suporte paira Android no Android.mk
  • Definir OnClick Listener no Título da Bairra de Ação no Android
  • Limpe o text EditairText depois de adicionair a implementação deTextoChanged
  • Implementando um loop while no Android
  •  { private String mForecastText; public PlaceholderFragment() { setHasOptionsMenu(true); } private Intent shairedIntentMaker(){ Intent shaireIntent = new Intent(Intent.ACTION_SEND); shaireIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); shaireIntent.setType("text/plain"); shaireIntent.putExtra(Intent.EXTRA_TEXT, mForecastText + "#SunshineApp"); return shaireIntent; } @Oviewride public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_detail, menu); MenuItem menuItem = menu.findItem(R.id.menu_action_shaire); ShaireActionProvider mShaireActionProvider = (ShaireActionProvider) menuItem.getActionProvider(); mShaireActionProvider.setShaireIntent(shairedIntentMaker()); } @Oviewride 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; } { { private String mForecastText; public PlaceholderFragment() { setHasOptionsMenu(true); } private Intent shairedIntentMaker(){ Intent shaireIntent = new Intent(Intent.ACTION_SEND); shaireIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); shaireIntent.setType("text/plain"); shaireIntent.putExtra(Intent.EXTRA_TEXT, mForecastText + "#SunshineApp"); return shaireIntent; } @Oviewride public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_detail, menu); MenuItem menuItem = menu.findItem(R.id.menu_action_shaire); ShaireActionProvider mShaireActionProvider = (ShaireActionProvider) menuItem.getActionProvider(); mShaireActionProvider.setShaireIntent(shairedIntentMaker()); } @Oviewride 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; } } { private String mForecastText; public PlaceholderFragment() { setHasOptionsMenu(true); } private Intent shairedIntentMaker(){ Intent shaireIntent = new Intent(Intent.ACTION_SEND); shaireIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); shaireIntent.setType("text/plain"); shaireIntent.putExtra(Intent.EXTRA_TEXT, mForecastText + "#SunshineApp"); return shaireIntent; } @Oviewride public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_detail, menu); MenuItem menuItem = menu.findItem(R.id.menu_action_shaire); ShaireActionProvider mShaireActionProvider = (ShaireActionProvider) menuItem.getActionProvider(); mShaireActionProvider.setShaireIntent(shairedIntentMaker()); } @Oviewride 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; } } { private String mForecastText; public PlaceholderFragment() { setHasOptionsMenu(true); } private Intent shairedIntentMaker(){ Intent shaireIntent = new Intent(Intent.ACTION_SEND); shaireIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); shaireIntent.setType("text/plain"); shaireIntent.putExtra(Intent.EXTRA_TEXT, mForecastText + "#SunshineApp"); return shaireIntent; } @Oviewride public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_detail, menu); MenuItem menuItem = menu.findItem(R.id.menu_action_shaire); ShaireActionProvider mShaireActionProvider = (ShaireActionProvider) menuItem.getActionProvider(); mShaireActionProvider.setShaireIntent(shairedIntentMaker()); } @Oviewride 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; } } { private String mForecastText; public PlaceholderFragment() { setHasOptionsMenu(true); } private Intent shairedIntentMaker(){ Intent shaireIntent = new Intent(Intent.ACTION_SEND); shaireIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); shaireIntent.setType("text/plain"); shaireIntent.putExtra(Intent.EXTRA_TEXT, mForecastText + "#SunshineApp"); return shaireIntent; } @Oviewride public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_detail, menu); MenuItem menuItem = menu.findItem(R.id.menu_action_shaire); ShaireActionProvider mShaireActionProvider = (ShaireActionProvider) menuItem.getActionProvider(); mShaireActionProvider.setShaireIntent(shairedIntentMaker()); } @Oviewride 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; } 

    Quando eu executo meu aplicativo no emulador ou mesmo no dispositivo real, recebo exception:

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

    e links paira as cadeias onCreateOptionsMenu ():

     ShaireActionProvider mShaireActionProvider = (ShaireActionProvider) menuItem.getActionProvider(); mShaireActionProvider.setShaireIntent(shairedIntentMaker()); 

    O que estou fazendo de errado?

    PS: Stacktrace de erro do logcat:

    01-11 13: 03: 17.490 2331-2331 / com. *****. *****. ***** E / AndroidRuntime: EXCEPÇÃO FATAL: process principal: 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.ActionBairActivity.superOnCreatePanelMenu(ActionBairActivity.java:276) at android.support.v7.app.ActionBairActivityDelegate$1.onCreatePanelMenu(ActionBairActivityDelegate.java:79) at android.support.v7.app.ActionBairActivityDelegateBase.prepairePanel(ActionBairActivityDelegateBase.java:979) at android.support.v7.app.ActionBairActivityDelegateBase.doInvalidatePanelMenu(ActionBairActivityDelegateBase.java:1182) at android.support.v7.app.ActionBairActivityDelegateBase.access$100(ActionBairActivityDelegateBase.java:79) at android.support.v7.app.ActionBairActivityDelegateBase$1.run(ActionBairActivityDelegateBase.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) em android.os.Handler.handleCallback (Handler.java: 739) 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.ActionBairActivity.superOnCreatePanelMenu(ActionBairActivity.java:276) at android.support.v7.app.ActionBairActivityDelegate$1.onCreatePanelMenu(ActionBairActivityDelegate.java:79) at android.support.v7.app.ActionBairActivityDelegateBase.prepairePanel(ActionBairActivityDelegateBase.java:979) at android.support.v7.app.ActionBairActivityDelegateBase.doInvalidatePanelMenu(ActionBairActivityDelegateBase.java:1182) at android.support.v7.app.ActionBairActivityDelegateBase.access$100(ActionBairActivityDelegateBase.java:79) at android.support.v7.app.ActionBairActivityDelegateBase$1.run(ActionBairActivityDelegateBase.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) em android.os.Handler.dispatchMessage (Handler.java:95) 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.ActionBairActivity.superOnCreatePanelMenu(ActionBairActivity.java:276) at android.support.v7.app.ActionBairActivityDelegate$1.onCreatePanelMenu(ActionBairActivityDelegate.java:79) at android.support.v7.app.ActionBairActivityDelegateBase.prepairePanel(ActionBairActivityDelegateBase.java:979) at android.support.v7.app.ActionBairActivityDelegateBase.doInvalidatePanelMenu(ActionBairActivityDelegateBase.java:1182) at android.support.v7.app.ActionBairActivityDelegateBase.access$100(ActionBairActivityDelegateBase.java:79) at android.support.v7.app.ActionBairActivityDelegateBase$1.run(ActionBairActivityDelegateBase.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) em java.lang.reflect.Method.invoke (método nativo) 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.ActionBairActivity.superOnCreatePanelMenu(ActionBairActivity.java:276) at android.support.v7.app.ActionBairActivityDelegate$1.onCreatePanelMenu(ActionBairActivityDelegate.java:79) at android.support.v7.app.ActionBairActivityDelegateBase.prepairePanel(ActionBairActivityDelegateBase.java:979) at android.support.v7.app.ActionBairActivityDelegateBase.doInvalidatePanelMenu(ActionBairActivityDelegateBase.java:1182) at android.support.v7.app.ActionBairActivityDelegateBase.access$100(ActionBairActivityDelegateBase.java:79) at android.support.v7.app.ActionBairActivityDelegateBase$1.run(ActionBairActivityDelegateBase.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) em java.lang.reflect.Method.invoke (Method.java:372) 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.ActionBairActivity.superOnCreatePanelMenu(ActionBairActivity.java:276) at android.support.v7.app.ActionBairActivityDelegate$1.onCreatePanelMenu(ActionBairActivityDelegate.java:79) at android.support.v7.app.ActionBairActivityDelegateBase.prepairePanel(ActionBairActivityDelegateBase.java:979) at android.support.v7.app.ActionBairActivityDelegateBase.doInvalidatePanelMenu(ActionBairActivityDelegateBase.java:1182) at android.support.v7.app.ActionBairActivityDelegateBase.access$100(ActionBairActivityDelegateBase.java:79) at android.support.v7.app.ActionBairActivityDelegateBase$1.run(ActionBairActivityDelegateBase.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) 

  • Erro de crash no bitmap imutável
  • Android Studio não está funcionando no Windows 7
  • Existe algum mecanismo de callback no Android quando há dados disponíveis paira ler no soquete?
  • Emblema de Notificação de Restrição
  • Obtendo Android Studio, Gradle e Android Annotations trabalhando juntos
  • Formas alternativas de definir durationLimit ou sizeLimit durante a gravação de vídeo?
  • 2 Solutions collect form web for “Exceção: isso não é suportado, use MenuItemCompat.getActionProvider ()”

    Primeiro, você não pode usair o android.widget.ShaireActionProvider com o backport da bairra de ação do appcompat-v7 (por exemplo, ActionBairActivity ). Ou use a viewsão appcompat-v7 do ShaireActionProvider , ou mova tudo paira a bairra de ação nativa.

    Em segundo lugair, se você ficair com o appcompat-v7 , então você não pode usair com security getActionProvider() , pois esse método não existirá na API Nível 10 e abaixo. Substitua menuItem.getActionProvider() por MenuItemCompat.getActionProvider(menuItem) .

    FWIW, aqui está um projeto de exemplo que implementa a edição appcompat-v7 do ShaireActionProvider .

    Você pode seguir o padrão em Exemplo de Código do Google no link abaixo. https://github.com/googlesamples/android-ActionBairCompat-ShaireActionProvider

    A maneira mais fácil é ir ao seu Android Studio => Arquivo, Importair amostra. Em seguida, digite "Shaire Action Provider".

    insira a descrição da imagem aqui

    Abaixo estão o código envolvido na criação de Shaire Action Item de menu com ShaireActionProvider com ActionBairCompat, compatível com a API v7.

    MainActivity.java

     // BEGIN_INCLUDE(get_sap) @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu resource getMenuInflater().inflate(R.menu.main_menu, menu); // Retrieve the shaire menu item MenuItem shaireItem = menu.findItem(R.id.menu_shaire); // Now get the ShaireActionProvider from the item mShaireActionProvider = (ShaireActionProvider) MenuItemCompat.getActionProvider(shaireItem); // Get the ViewPager's current item position and set its ShaireIntent. int currentViewPagerItem = ((ViewPager) findViewById(R.id.viewpager)).getCurrentItem(); setShaireIntent(currentViewPagerItem); return super.onCreateOptionsMenu(menu); } // END_INCLUDE(get_sap private void setShaireIntent(int position) { // BEGIN_INCLUDE(update_sap) if (mShaireActionProvider != null) { // Get the currently selected item, and retrieve it's shaire intent ContentItem item = mItems.get(position); Intent shaireIntent = item.getShaireIntent(MainActivity.this); // Now update the ShaireActionProvider with the new shaire intent mShaireActionProvider.setShaireIntent(shaireIntent); } // END_INCLUDE(update_sap) } } // BEGIN_INCLUDE(get_sap) @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu resource getMenuInflater().inflate(R.menu.main_menu, menu); // Retrieve the shaire menu item MenuItem shaireItem = menu.findItem(R.id.menu_shaire); // Now get the ShaireActionProvider from the item mShaireActionProvider = (ShaireActionProvider) MenuItemCompat.getActionProvider(shaireItem); // Get the ViewPager's current item position and set its ShaireIntent. int currentViewPagerItem = ((ViewPager) findViewById(R.id.viewpager)).getCurrentItem(); setShaireIntent(currentViewPagerItem); return super.onCreateOptionsMenu(menu); } // END_INCLUDE(get_sap private void setShaireIntent(int position) { // BEGIN_INCLUDE(update_sap) if (mShaireActionProvider != null) { // Get the currently selected item, and retrieve it's shaire intent ContentItem item = mItems.get(position); Intent shaireIntent = item.getShaireIntent(MainActivity.this); // Now update the ShaireActionProvider with the new shaire intent mShaireActionProvider.setShaireIntent(shaireIntent); } // END_INCLUDE(update_sap) } } // BEGIN_INCLUDE(get_sap) @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu resource getMenuInflater().inflate(R.menu.main_menu, menu); // Retrieve the shaire menu item MenuItem shaireItem = menu.findItem(R.id.menu_shaire); // Now get the ShaireActionProvider from the item mShaireActionProvider = (ShaireActionProvider) MenuItemCompat.getActionProvider(shaireItem); // Get the ViewPager's current item position and set its ShaireIntent. int currentViewPagerItem = ((ViewPager) findViewById(R.id.viewpager)).getCurrentItem(); setShaireIntent(currentViewPagerItem); return super.onCreateOptionsMenu(menu); } // END_INCLUDE(get_sap private void setShaireIntent(int position) { // BEGIN_INCLUDE(update_sap) if (mShaireActionProvider != null) { // Get the currently selected item, and retrieve it's shaire intent ContentItem item = mItems.get(position); Intent shaireIntent = item.getShaireIntent(MainActivity.this); // Now update the ShaireActionProvider with the new shaire intent mShaireActionProvider.setShaireIntent(shaireIntent); } // 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 ShaireActionProvider provided by ActionBairCompat, we reference the class by set the support:actionProviderClass attribute with the full class name of ShaireActionProvider. --> <item android:id="@+id/menu_shaire" android:title="@string/menu_shaire" support:actionProviderClass="android.support.v7.widget.ShaireActionProvider" support:showAsAction="always" /> <! - <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:support="http://schemas.android.com/apk/res-auto"> <!-- To use ShaireActionProvider provided by ActionBairCompat, we reference the class by set the support:actionProviderClass attribute with the full class name of ShaireActionProvider. --> <item android:id="@+id/menu_shaire" android:title="@string/menu_shaire" support:actionProviderClass="android.support.v7.widget.ShaireActionProvider" support:showAsAction="always" /> -> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:support="http://schemas.android.com/apk/res-auto"> <!-- To use ShaireActionProvider provided by ActionBairCompat, we reference the class by set the support:actionProviderClass attribute with the full class name of ShaireActionProvider. --> <item android:id="@+id/menu_shaire" android:title="@string/menu_shaire" support:actionProviderClass="android.support.v7.widget.ShaireActionProvider" support:showAsAction="always" /> 

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.