Unable to cast Action Provider to Share Action Provider
I had the same problem and I have found the solution:
1) You have to 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);
ActionProvider cannot be converted to ShareActionProvider
android.widget.ShareActionProvider
and android.support.v7.widget.ShareActionProvider
are not the same class. Change your import to match your actionProviderClass
.
Cannot cast from ActionProvider to ShareActionProvider
You should be using the ShareActionProvider
from ABS, not the stock one.
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
Null Pointer exception in using support library share action provider
I found the error. The problem was that support library requires to have a custom prefix and not android:actionProviderClass
. What i was doing wrong that i used android:actionProviderClass
instead of customprefix:actionProviderClass
See here: https://developer.android.com/training/basics/actionbar/adding-buttons.html
Android: getMenuInflater().inflate throws android.widget.ShareActionProvider cannot be cast to android.support.v4.view.ActionProvider
Replace:
oceanbook:actionProviderClass="android.widget.ShareActionProvider"
with:
oceanbook:actionProviderClass="android.support.v7.widget.ShareActionProvider"
If you are using the appcompat-v7
action bar backport, you have to use the ShareActionProvider
for the appcompat-v7
action bar backport. Since you are inheriting from AppCompatActivity
, you are using this backport.
Detecting sharer app when using share action provider
UPDATE:
The simplest solution is:
@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
String shareTarget = intent.getComponent().getPackageName();
...
}
No need to copy files or anything.
Copy some files from either the Android source, or ActionBarSherlock, if you happen to use the latter:
- ActivityChooserModel.java
- ActivityChooserView.java
- ShareActionProvider.java
Make sure you reference these files, not the original ones from your app.
In ActivityChooserModel.java, modify this:
if (mActivityChoserModelPolicy != null) {
ResolveInfo info = getActivity(index);
choiceIntent.putExtra("user_selected_activity", (info.activityInfo != null) ? info.activityInfo.packageName : info.serviceInfo.packageName);
final boolean handled = mActivityChoserModelPolicy.onChooseActivity(this, choiceIntent);
if (handled)
return null;
}
and it will store the package name of the selected activity into the intent. You can then read it in your handler:
@Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
String shareTarget = intent.getStringExtra("user_selected_activity");
...
}
and decide what to handle differently depending on the activity selected.
Cannot cast 'ActionProvider' to 'MediaRouteActionProvider'
Make sure you are importing MediaRouter from the MediaRouter support library and not the one from the android framework itself.
Related Topics
Extracting Information from a Scanned Gs1-Type Barcode
Cannot Resolve Mapactivity Class on Android
Android Marshmallow: Changing Permissions at Run Time Crashes App
How to Create and Save a Screenshot from a Surfaceview
Android Spinner Error:Android.View.Windowmanager$Badtokenexception: Unable to Add Window
Ndk: How Include *.So Files in Androidstudio
Why Does Navigation Not Work in the Navigation Drawer Activity Template with Version 2.4.1
How to Animate a Slide in Notification View That Pushes the Content View Down
Sdcard Content Exist But Cant See Them
Multiple Dex Files Define Lcom/Google/Android/Gms/Internal/Zzau
How to Pass Custom Component Parameters in Java and Xml
Getting Height of Text View Before Rendering to Layout
How to Use Sms Content Provider? Where Are the Docs
Determine Angle of View of Smartphone Camera
Error in Strings.Xml File in Android