Cannot cast from ActionProvider to ShareActionProvider
Asked Answered
S

1

10

I am trying to implement a ShareActionProvider in my ActionBarSherlock.

The following code gives me this error :

Cannot cast from ActionProvider to ShareActionProvider

ReadingActivity.java

@Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu2b, (Menu) menu);
            menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        menu.getItem(1).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        MenuItem menuItem = menu.findItem(R.id.share);
          // Get the provider and hold onto it to set/change the share intent.
        ShareActionProvider shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();

        // Set the default share intent
        shareActionProvider.setShareIntent(getDefaultShareIntent());
        return super.onCreateOptionsMenu(menu);
    }

menu2b.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <item android:id="@+id/share"
          android:title="Share"
          android:icon="@drawable/ic_share_action_bar"
          android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>
Salify answered 29/9, 2012 at 16:32 Comment(0)
S
14

You should be using the ShareActionProvider from ABS, not the stock one.

android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
Stempien answered 30/9, 2012 at 4:44 Comment(2)
Thanks for your quick response, but Eclipse doesn't seem to recognise my "import com.actionbarsherlock.widget.ShareActionProvider;", i get "The import com.actionbarsherlock.widget cannot be resolved"Salify
Are you using the latest version of ABS?Stempien

© 2022 - 2024 — McMap. All rights reserved.