Why Menuitemcompat.Getactionprovider Returns Null

getActionProvider: item does not implement SupportMenuItem; returning null

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

ShareActionProvider is null

You could just create a ShareActionProvider and assign it.

mShareActionProvider = new ShareActionProvider();
mShareActionProvider.setShareIntent(createShareIntent())
MenuItemCompat.setActionProvider(item, mShareActionProvider);

How to resolve Null while setting ShareActionProvider from getActionProvider()?

You should be considering Compatibility factor here.

(ShareActionProvider) menuItem.getActionProvider() - this gives NULL as per the code in the question.

follow changes in the code below, app will work.

Menu

  <item
android:id="@+id/action_share"
android:title="share"
android:orderInCategory="2"
app:showAsAction="ifRoom"
app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider" />

Manifest file

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

Activity

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ShareActionProvider;
import androidx.core.view.MenuItemCompat;

public class ShareActivity extends AppCompatActivity {
private ShareActionProvider shareActionProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
}

@Override
public boolean onCreateOptionsMenu(Menu menu ){
getMenuInflater().inflate(R.menu.menu_main, menu);//dette legger til menyens
MenuItem menuItem = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
setIntent("this is example text");
return super.onCreateOptionsMenu(menu);
}

private void setIntent(String text) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
shareActionProvider.setShareIntent(intent);//TODO Feil med setShareIntent metoden
}

Exception: This is not supported, use MenuItemCompat.getActionProvider()

First, you cannot use android.widget.ShareActionProvider with the appcompat-v7 action bar backport (e.g., ActionBarActivity). Either use the appcompat-v7 version of ShareActionProvider, or move everything over to the native action bar.

Second, if you stick with appcompat-v7, then you cannot safely use getActionProvider(), as that method will not exist on API Level 10 and below. Replace menuItem.getActionProvider() with MenuItemCompat.getActionProvider(menuItem).

FWIW, here is a sample project that implements the appcompat-v7 edition of ShareActionProvider.

getActionProvider: item does not implement SupportMenuItem

The problem is that the MultipleModeListener interface extends the android.view.ActionMode.Callback, as can be seen in the source code at http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/widget/AbsListView.java#6301. If you are using ShareActionProvider from the support library, you need the android.support.v7.view.ActionMode.Callback instead.

The solution is to create your own ActionMode.CallBack implementation instead of using the framework's MultipleModeListener. This way you make sure that the support libraries are being used wherever required.

For example :

  1. Import the v7 version of ActionMode and ActionBarActivity in your fragment

    import android.support.v7.view.ActionMode;
    import android.support.v7.app.ActionBarActivity;
  2. Create an onClickListener for your list view and use startSupportActionMode to start your custom ActionMode.CallBack implementation

    myListView.setItemsCanFocus(false);
    myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    actionMode = null;
    myListView.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    if(myListView.getCheckedItemCount() == 0){
    actionMode.finish();
    return;
    }

    if(actionMode == null){
    actionMode = ((ActionBarActivity)getActivity()).startSupportActionMode(new ContextualActionBar());
    }

    }
    });
  3. Create your custom ActionMode.Callback implementation

    private class ContextualActionBar implements ActionMode.Callback{
    private ShareActionProvider mShareActionProvider;

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch(item.getItemId()){

    case R.id.share_menu :
    mode.finish();
    return true;

    default :
    return false;
    }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.chatsession_contextmenu, menu);

    //Initialize the ShareActionProvider
    MenuItem shareMenuItem = menu.findItem(R.id.share_menu);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, "test message");
    mShareActionProvider.setShareIntent(shareIntent);
    return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
    //Nullify the actionMode object
    //so that the onClickListener can identify whether the ActionMode is ON
    actionMode = null;

    //Uncheck all checked messages
    SparseBooleanArray selectedItems = myListView.getCheckedItemPositions();
    for(int i=0;i<selectedItems.size();i++){
    myListView.setItemChecked(selectedItems.keyAt(i), false);
    }
    }

    @Override
    public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
    // TODO Auto-generated method stub
    return false;
    }

    }


Related Topics



Leave a reply



Submit