How to Cast Action Provider to Share Action Provider

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



Leave a reply



Submit