Search code examples
androidandroid-intentandroid-appcompatshare-intent

getActionProvider: item does not implement SupportMenuItem; returning null


I am using appcompat-v7:22.2.0' and an AppCompatActivity. When trying to create a share intent I get the following warning:

W/MenuItemCompat: getActionProvider: item does not implement SupportMenuItem; returning null

In addition while the share icon is present in the menu nothing happens when its tapped, nothing, not even an error. Maybe this is the only problem i.e. hooking up the tap?

It looks like I am missing some quirk of the appcompat support library but I can't find the relevant documentation

The code in my activity

@Override                                                                                    
public boolean onCreateOptionsMenu(Menu menu) {                                              
    ...                                                                                 
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
    setShareIntent();                                                                        
    ...                                                 
}                                                                                            

private ShareActionProvider mShareActionProvider; 

private void setShareIntent() {                                                                        
    SubsamplingScaleImageView tempImage = (SubsamplingScaleImageView) findViewById(R.id.thumbnailView);
    if (mShareActionProvider != null && tempImage != null) {                                           

        Log.e(TAG, "this happened");                                                                   
        // Get access to the URI for the bitmap                                                        
        Uri bmpUri = getLocalBitmapUri(tempImage);                                                     
        if (bmpUri != null) {                                                                          
            // Construct a ShareIntent with link to image                                              
            Intent shareIntent = new Intent();                                                         
            shareIntent.setAction(Intent.ACTION_SEND);                                                 
            shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);                                         
            shareIntent.setType("image*//*");                                                          
            mShareActionProvider.setShareIntent(shareIntent);                                          
        } else {                                                                                       
            // ...sharing failed, handle error                                                         
        }                                                                                              
    }else{                                                                                             
        Log.e(TAG, "this should not have happened");                                                   
    }                                                                                                  
} 

I tried casting to but this made no difference

mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider((SupportMenuItem) shareItem); 

imports used

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.view.MenuItemCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.ShareActionProvider;
import android.widget.ImageView;  

EDIT: I tried this with an ActionBarActivity in case that was the problem but no change.


Solution

  • So it turns out that this error was leading me down the garden path. The problem turned out to be that I was using the wrong menu layout for the fragment I was working in. Very confusing.

    If mods would rather I delete this question just let me know