getActionProvider:项目未实现SupportMenuItem [英] getActionProvider: item does not implement SupportMenuItem

查看:248
本文介绍了getActionProvider:项目未实现SupportMenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用支持库在我的片段的上下文操作栏实施 ShareActionProvider 。我的脸在正常操作栏实现它没有任何问题(onCreateOptionsMenu()),但是当我尝试在CAB(onCreateActionMode()在 MultiModeListener 接口),我得到的错误:

I am trying to implement a ShareActionProvider using the support library in a contextual action bar in my fragment. I face no issues implementing it in a normal action bar( onCreateOptionsMenu() ), but when i try it in the CAB ( onCreateActionMode() in MultiModeListener interface), I get the error :

getActionProvider: item does not implement SupportMenuItem; returning null

纵观Android源的<一个href="https://cells-source.cs.columbia.edu/plugins/gitiles/platform/frameworks/support/+/30837f1095c803f332f4a1c3f0917c8afdd50156/v4/java/android/support/v4/view/MenuItemCompat.java" rel="nofollow">https://cells-source.cs.columbia.edu/plugins/gitiles/platform/frameworks/support/+/30837f1095c803f332f4a1c3f0917c8afdd50156/v4/java/android/support/v4/view/MenuItemCompat.java,这个问题似乎是因为我的菜单项不是一个实例 SupportMenuItem

Looking at the Android source at https://cells-source.cs.columbia.edu/plugins/gitiles/platform/frameworks/support/+/30837f1095c803f332f4a1c3f0917c8afdd50156/v4/java/android/support/v4/view/MenuItemCompat.java, the problem seems to be because my MenuItem is not an instance of 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;
}

我怎么能去解决这个任何想法?

Any ideas on how i can go about resolving this ?

清单:

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

活动:

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

    }
}

片段:

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模式,菜单菜单) onCreateOptionsMenu(菜单菜单,MenuInflater充气)。只有一个在 onCreateOptionsMenu MenuWrapperICS 。下面是两个对象在调试模式下的截图:

The root of the problem seems to be the difference in the Menu object that is being passed as argument to onCreateActionMode(ActionMode mode, Menu menu) and onCreateOptionsMenu(Menu menu, MenuInflater inflater). Only the one in onCreateOptionsMenu has the MenuWrapperICS. Here is a screenshot of both objects in debug mode :

onCreateActionMode(ActionMode mode, Menu menu) :

onCreateOptionsMenu(Menu menu, MenuInflater inflater) :

推荐答案

的问题是, MultipleModeListener 接口扩展了 android.view.ActionMode .Callback ,因为可以在<一中可以看出,在源$ C ​​$ C href="http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/widget/AbsListView.java#6301" rel="nofollow">http://androidxref.com/4.4.2_r2/xref/frameworks/base/core/java/android/widget/AbsListView.java#6301.如果您使用的是 ShareActionProvider 从支持库,你需要的 android.support.v7.view.ActionMode.Callback 代替。

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.

解决方案是创建一个使用该框架的 MultipleModeListener 你自己ActionMode.CallBack实现替代。这样,您就确保了支持库被用于任何需要。

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.

例如:

  1. 导入 ActionMode 的V7版本, ActionBarActivity 在片段

import android.support.v7.view.ActionMode;
import android.support.v7.app.ActionBarActivity;

  • 创建一个 onClickListener 为您的列表视图,并使用 startSupportActionMode 来开始您的自定义 ActionMode.CallBack 实施

  • 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());
                        }
    
                }
            });
    

  • 创建自定义的 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;
        }
    
    }
    

  • 这篇关于getActionProvider:项目未实现SupportMenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆