gpt4 book ai didi

android - getActionProvider : item does not implement SupportMenuItem

转载 作者:太空狗 更新时间:2023-10-29 16:03:20 27 4
gpt4 key购买 nike

我正在尝试在 fragment 的上下文操作栏中使用支持库来实现 ShareActionProvider。我在正常操作栏( onCreateOptionsMenu() )中实现它时没有遇到任何问题,但是当我在 CAB 中尝试它时( MultiModeListener 接口(interface)中的 onCreateActionMode() ),我收到错误:

getActionProvider: item does not implement SupportMenuItem; returning null

https://cells-source.cs.columbia.edu/plugins/gitiles/platform/frameworks/support/+/30837f1095c803f332f4a1c3f0917c8afdd50156/v4/java/android/support/v4/view/MenuItemCompat.java 查看 Android 源代码,问题似乎是因为我的 MenuItem 不是 SupportMenuItem 的实例:

 public static ActionProvider getActionProvider(MenuItem item) {
if (item instanceof SupportMenuItem) {
return ((SupportMenuItem) item).getSupportActionProvider();
}

// TODO Wrap the framework ActionProvider and return it
Log.w(TAG, "getActionProvider: item does not implement SupportMenuItem; returning null");
return null;
}

关于如何解决这个问题有什么想法吗?

list :

<activity
android:name=".myactivity_ActionBarActivity"
android:theme="@style/Theme.AppCompat.Light"
android:windowSoftInputMode="stateUnchanged">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Activity :

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

public class myactivity_ActionBarActivity extends ActionBarActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbaractivity_layout); //loads a fragment

}
}

fragment :

import android.support.v7.widget.ShareActionProvider;
import android.support.v4.view.MenuItemCompat;
import android.view.MenuItem;
import android.view.Menu;
import android.support.v4.app.Fragment;

...
...

@Override
public void onActivityCreated(Bundle savedInstanceState) {
...
...
//Handle Action mode events
myListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
private ShareActionProvider mShareActonProvider;
....
....
@Override
public boolean onCreateActionMode(ActionMode mode,
Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.chatsession_contextmenu, menu);

//get the ShareActionProvider from the menu item
MenuItem item = menu.findItem(R.id.share_menu);
mShareActonProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

return true;
}
}

...
...
}

菜单布局文件:

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/delete_menu"
android:title="Delete message"
myapp:showAsAction="ifRoom|withText"
android:icon="@drawable/ic_action_discard">
</item>

<item
android:id="@+id/share_menu"
android:title="Share message"
myapp:showAsAction="ifRoom|withText"
android:icon="@drawable/ic_action_share"
myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider">
</item>

</menu>

编辑 1:

问题的根源似乎是作为参数传递给 onCreateActionMode(ActionMode mode, Menu menu)Menu 对象的差异onCreateOptionsMenu(菜单菜单,MenuInflater 充气器)。只有 onCreateOptionsMenu 中的那个有 MenuWrapperICS。这是 Debug模式下两个对象的屏幕截图:

onCreateActionMode(ActionMode mode, Menu menu) :

enter image description here

onCreateOptionsMenu(Menu menu, MenuInflater inflater) :

enter image description here

最佳答案

问题在于 MultipleModeListener 接口(interface)扩展了 android.view.ActionMode.Callback,如源代码所示 http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/widget/AbsListView.java#6301 .如果您使用支持库中的 ShareActionProvider,则需要 android.support.v7.view.ActionMode.Callback

解决方案是创建您自己的 ActionMode.CallBack 实现,而不是使用框架的 MultipleModeListener。通过这种方式,您可以确保在需要的地方使用支持库。

例如:

  1. 在您的 fragment 中导入 v7 版本的 ActionModeActionBarActivity

    import android.support.v7.view.ActionMode;
    import android.support.v7.app.ActionBarActivity;
  2. 为您的 ListView 创建一个 onClickListener 并使用 startSupportActionMode 启动您的自定义 ActionMode.CallBack 实现

    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. 创建自定义 ActionMode.Callback 实现

    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;
    }

    }

关于android - getActionProvider : item does not implement SupportMenuItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219842/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com