Unable to cast Action Provider to Share Action Provider

后端 未结 8 1064
一个人的身影
一个人的身影 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.

提交回复
热议问题