Unable to transfer Action Provider to action provider

Below is the code of my activity

import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.ShareActionProvider; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { private ShareActionProvider shareAction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); MenuItem item = menu.getItem(R.id.menu_settings); shareAction = (ShareActionProvider) item.getActionProvider(); return true; } } 

The problem is that there is a personal error in ActionProvider for ShareActionProvider. Why is it so lower - activity_menu.xml

  <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_settings" android:orderInCategory="100" android:showAsAction="always" android:title="@string/menu_settings" android:actionProviderClass="android.widget.ShareActionProvider" /> </menu> 
+59
android shareactionprovider
01 Oct '13 at 13:53 on
source share
8 answers

I had the same problem and found a solution:

1) You must use:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bwq="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/menu_share" android:title="@string/menu_share" bwq:actionProviderClass="android.support.v7.widget.ShareActionProvider" bwq:showAsAction="always"/> </menu> 

2) and in Java

 import android.support.v7.widget.ShareActionProvider; 

and

 // Locate MenuItem with ShareActionProvider MenuItem item = menu.findItem(R.id.menu_share); // Fetch and store ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item); 
+206
Nov 18 '13 at 12:22
source share

menu:

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

Java:

 MenuItem menuItem = menu.findItem(R.id.action_share); mActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 
+8
Oct 17 '16 at 14:48
source share

You are using android.widget.ShareActionProvider , which is for the API level 11+ action bar. If you use the AppCompat auxiliary port in the action bar, you need to use android.support.v7.widget.ShareActionProvider instead.

+7
01 Oct '13 at 14:03
source share

I ran into this problem following the android action dev android file , which is basically what you are doing. After you made your way into samples that use the action bar using the backward compatible v7 and v4 support libraries, I ended up using the following code for onCreateOptionsMenu ().

 @Override public boolean onCreateOptionsMenu(Menu menu) { File file = new File(mFilePath); ShareCompat.IntentBuilder b = ShareCompat.IntentBuilder.from(this) .setType("image/png") .setStream(Uri.fromFile(file)); MenuItem item = menu.add("Share"); ShareCompat.configureMenuItem(item, b); MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); return true; } 

A few things to note here are that you are not pouting from a menu resource. A default sharing button has been added to the menu. You just need to indicate what type of resource you are using with .setType. Since I am sharing the file, I need setStream, with Uri.fromFile (new File ()); If you shared text, you would set Type ("text / plain").

Also make sure you import the $ SDK \ extras \ android \ support \ v7 \ appcompat library project, which contains the necessary packages. In addition, since they imported this library project, your project does not need v4support.jar in the libs folder, because the library project already has it.

+3
Dec 24 '13 at 5:59
source share

The problem was that what @CommonsWare was saying that it was not using the ShareActionProvider support library, and even if I did, it also did not work, because when using the support library we need custom prefixes for some actions, such as showAsAction

+1
Oct. 15 '13 at 18:02
source share

Follow the simple rule that I found useful.

With AppCompatActivity use this ,

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:support="http://schemas.android.com/apk/res-auto"> <!-- To use ShareActionProvider, 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" /> </menu> 

You can also replace support : actionProviderClass with app: actionProviderClass and support: showAsAction with app: showAsAction

In your onCreateOptionsMenu ()

 @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); //set its ShareIntent. setShareIntent(shareIntent); return super.onCreateOptionsMenu(menu); } 

With Activity use this ,

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/share" android:actionProviderClass="android.widget.ShareActionProvider" android:showAsAction="ifRoom" tools:ignore="MenuTitle" /> </menu> 

In your onCreateOptionsMenu()

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.actions, menu); ShareActionProvider share= (ShareActionProvider)menu.findItem(R.id.share) .getActionProvider(); share.setShareIntent(shareIntent); return(super.onCreateOptionsMenu(menu)); } 
0
Aug 25 '18 at 11:22
source share

None of the solutions here solved my problem with ShareActionProvider without casting / not returning null. I ended up replacing ShareActionProvider with Intent.SEND_ACTION to share the images in my application, as shown in the Android tutorial for developers: https://developer.android.com/training/sharing/send

Although Google mentions in this guide that: Note: The best way to add a share action item to an ActionBar is to use ShareActionProvider, which became available in API level 14. ShareActionProvider is discussed in the lesson about Adding an Easy Share Action. I found it much easier to implement only Intention.SEND_ACTION. Not sure if there are other reasons for implementing ShareActionProvider ...

0
Jun 24 '19 at 5:58
source share

This was asked a few years ago, so the existing answers probably worked then. However, at the time of this writing, the proposed code gives a lot of warnings about obsolescence, and this does not solve the problem.

In the end, I solved the problem, and it was not documented anywhere on the Internet (what I could find), so I hope this answer helps people who are currently facing the same problem.

The solution for me was in the import application. When I first used SharedActionProvider, Android Studio can automatically add imports. It provides two options for import: android.widget.ShareActionProvider and androidx.appcompat.widget.ShareActionProvider .

The first is violated and results in an error that the cast will never be successful. The latter will make everything work correctly. app:ActionProviderClass in the menu file must match the name of the imported file.

0
Jul 28 '19 at 7:21
source share



All Articles