Unable to cast Action Provider to Share Action Provider
Asked Answered
E

8

59

Below is the code for 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 cast error from ActionProvider to ShareActionProvider.Why it is so below is 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>
Edgeworth answered 1/10, 2013 at 13:53 Comment(0)
C
206

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);
Constrict answered 18/11, 2013 at 12:22 Comment(0)
T
8

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);
Talkathon answered 17/10, 2016 at 14:48 Comment(0)
S
7

You are using android.widget.ShareActionProvider, which is for the native API Level 11+ action bar. If you are using the AppCompat backport of the action bar, you need to use android.support.v7.widget.ShareActionProvider instead.

Showroom answered 1/10, 2013 at 14:3 Comment(2)
But error is in the cast of ActionProvider to ShareActionProvider. And i m using android.support.v7.widget.ShareActionProviderEdgeworth
@AnuranjitMaindola: " And i m using android.support.v7.widget.ShareActionProvider" -- not according to your question. In your question, you have android:actionProviderClass="android.widget.ShareActionProvider". That will not work with import android.support.v7.widget.ShareActionProvider. Either both need to have support.v7, or both need to not have it.Showroom
O
3

I ran into this problem following the android dev actionbar guide which is basically what you are doing. After digging into the samples that utilize the action bar using the backwards 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;
    }

Couple of things to note here is that you are not inflating from a menu resource. The menu is having the default share button added to it. You simply need to specify what type of resource your are sharing with .setType. Since I am sharing a file I need to setStream, with Uri.fromFile(new File()); If you were sharing text you would setType("text/plain").

Also make sure you have imported the $SDK\extras\android\support\v7\appcompat library project, which contains the needed packages. Also since have imported that library project, your project does not need the v4support.jar in your libs folder because the library project already has it.

Optometer answered 24/12, 2013 at 5:59 Comment(0)
D
2

This was asked years ago, so the existing answers probably worked back then. However, at the time of this writing the suggested code gives many deprecation warnings and it doesn't solve the problem.

I did eventually solve the problem and it was not documented anywhere on the web (that I could find), so hopefully this answer will help people who currently run into this same problem.

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

The former is broken and results in the error about the cast never succeeding. The latter will make everything work properly. The app:ActionProviderClass in the menu file must be identical to the imported file name.

Dammar answered 28/7, 2019 at 7:21 Comment(0)
E
1

The problem was that what @CommonsWare said about not using the support library ShareActionProvider and also even if i did then it also wont have worked out because when using Support Library we require Custom Prefixes for some actions like showAsAction

Edgeworth answered 15/10, 2013 at 18:2 Comment(0)
A
0

Follow a 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));
  }
Aarika answered 25/8, 2018 at 11:22 Comment(0)
C
0

None of the solutions here solved my problem with ShareActionProvider, not casting / returning null. I ended up replacing ShareActionProvider with just an Intent.SEND_ACTION to share images in my app, like presented in Android Developers tutorial: https://developer.android.com/training/sharing/send

Although Google mentions in this tutorial 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 much simpler to implement just the Intention.SEND_ACTION. Not sure if there are other reasons to implement the ShareActionProvider...

Cryptozoite answered 24/6, 2019 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.