Unable to cast Action Provider to Share Action Provider

后端 未结 8 1038
一个人的身影
一个人的身影 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:35

    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

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

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