Unable to cast Action Provider to Share Action Provider

后端 未结 8 1037
一个人的身影
一个人的身影 2020-11-29 19:58

Below is the code for my Activity

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v7.widget.ShareActionProvider;
          


        
相关标签:
8条回答
  • 2020-11-29 20:14

    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.

    0 讨论(0)
  • 2020-11-29 20:16

    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...

    0 讨论(0)
  • 2020-11-29 20:21

    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);
    
    0 讨论(0)
  • 2020-11-29 20:21

    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.

    0 讨论(0)
  • 2020-11-29 20:22

    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.

    0 讨论(0)
  • 2020-11-29 20:32

    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));
      }
    
    0 讨论(0)
提交回复
热议问题