why MenuItemCompat.getActionProvider returns null?

前端 未结 12 1031
天命终不由人
天命终不由人 2020-11-29 06:33

I tried to use android.support.v7.widget.ShareActionProvider on actionbar in my app. So I followed the example from android document but got some issues.
Here\'s my m

相关标签:
12条回答
  • 2020-11-29 07:01

    Here this is the only solution that works to make ShareActionProvider not null...i use set ActionProvider instead...see the code below:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.messages_activity_menu, menu);
        MenuItem menuItem = menu.findItem(R.id.menu_item_share);
        shareActionProvider = new ShareActionProvider(this);
        MenuItemCompat.setActionProvider(menuItem, shareActionProvider);
    
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        if(item.getItemId() == R.id.menu_item_share){
            onShareAction();
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    private void onShareAction(){
        // Create the share Intent
        String playStoreLink = "https://play.google.com/store/apps/details?id=" + getPackageName();
        String yourShareText = getResources().getString(R.string.share_text) + playStoreLink;
        Intent shareIntent = ShareCompat.IntentBuilder.from(this).setType("text/plain").setText(yourShareText).getIntent();
        // Set the share Intent
        if (shareActionProvider != null) {
            shareActionProvider.setShareIntent(shareIntent);
        }
    }
    

    and...xml

     <?xml version="1.0" encoding="utf-8"?>
     <menu xmlns:android="http://schemas.android.com/apk/res/android" >
         <item
        android:id="@+id/menu_item_share"
        android:icon="@drawable/ic_action_share"
        android:showAsAction="ifRoom|withText"
        android:title="@string/menu_item_share" />
     </menu>
    

    and other things that may be checked:

    the activity has to extends ActionBarActivity:

    MyActivity extends ActionBarActivity
    

    check and use this imports:

    import android.support.v4.app.ShareCompat;
    import android.support.v4.view.MenuItemCompat;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBar.OnNavigationListener;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.widget.ShareActionProvider;
    

    In AndroidManifest.xml put this line in your activity's tag attributes:

    android:theme="@style/Theme.AppCompat.Light"
    

    If you dont know how to import v7 and v4 compatibility libraries see: http://developer.android.com/tools/support-library/setup.html

    0 讨论(0)
  • 2020-11-29 07:02

    I had the same type of error. MenuItemCompat.getActionProvider returned null.

    My problem was in ProGuard. Turning ProGuard off solved it for me.

    0 讨论(0)
  • 2020-11-29 07:03

    There is another potential related problem. If your MenuItem is always present in the ActionBar, using the following code:

    menuItem?.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) 
    

    the resulting Menu item won't work (it will be visible, but not enabled). There are two options, either change the ShowAsAction type or call setShareIntent directly in the onCreateOptionsMenu() function:

    this@MyAwesomeActivity.runOnUiThread(java.lang.Runnable {
         actionProvider.setShareIntent(shareIntent)
    })
    
    0 讨论(0)
  • 2020-11-29 07:05

    You should change the declaration and definition of onCreateOptionsMenu function. you have changed the function declaration from that of the base class. this means the method is not overridden.

    Try this :

    @Override
    public boolean onCreateOptionsMenu (Menu menu) {
    
        inflater.inflate(R.menu.share, menu);// declare a local layout inflater
        MenuItem shareItem = menu.findItem(R.id.action_share);
        ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);
        mShareActionProvider.setShareIntent(getDefaultIntent());
    //    super.onCreateOptionsMenu(menu, inflater);   <--- Remove this line or put in the first line because base class constructor should be called in the first line of the method.
    
    return super.onCreateOptionsMenu(menu);//<-- Add this line to set the menu after adding menu items
    }
    
    0 讨论(0)
  • 2020-11-29 07:07

    Try to add to unit of activity

    import android.support.v4.view.MenuItemCompat;
    

    I've had the same problem and it was solution.

    0 讨论(0)
  • 2020-11-29 07:08

    If someone wants to keep progaurd on and still use the code:

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

    Just need to add to proguard:

    -keep class android.support.v7.widget.ShareActionProvider { *; }
    
    0 讨论(0)
提交回复
热议问题