getActionProvider:item未实现SupportMenuItem

时间:2014-06-14 12:17:19

标签: android android-support-library

我正在尝试使用片段中的上下文操作栏中的支持库来实现ShareActionProvider。我在正常的操作栏(onCreateOptionsMenu())中没有遇到任何问题,但是当我在CAB中尝试它时(MultiModeListener接口中的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;
}

关于我如何解决这个问题的任何想法?

清单:

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

问题的根源似乎是作为MenuonCreateActionMode(ActionMode mode, Menu menu)的参数传递的onCreateOptionsMenu(Menu menu, MenuInflater inflater)对象的差异。只有onCreateOptionsMenu中的MenuWrapperICS具有onCreateActionMode(ActionMode mode, Menu menu) : 。以下是调试模式下两个对象的屏幕截图:

onCreateOptionsMenu(Menu menu, MenuInflater inflater) :

enter image description here

{{1}}

enter image description here

4 个答案:

答案 0 :(得分:5)

问题是MultipleModeListener界面扩展了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. 在您的片段中导入ActionModeActionBarActivity的v7版本

    import android.support.v7.view.ActionMode;
    import android.support.v7.app.ActionBarActivity;
    
  2. 为列表视图创建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;
        }
    
    }
    

答案 1 :(得分:2)

在onCreateActionMode中填充IMenu时,请小心使用正确的MenuInflater。当我使用模式对象中的那个时,就像在您的片段类中一样,它不会创建ShareActionProvider的支持版本。我切换到使用来自父AppCompatActivity类的MenuInflater,它工作正常。

鉴于模式对象来自支持库,可以假设它将使用支持inflater,但显然不是。

答案 2 :(得分:0)

您确定自己的活动延伸了ActionBarActivity吗?

答案 3 :(得分:0)

我的设置几乎相同。我的问题是Proguard正在优化ShareActionProvider的构造函数。有一个错误,他们确实在XML中检测到ActionProvider类的类和方法时,他们不会保留构造函数或类名。

如果你有&#34;无法实例化课程&#34;在您的日志中发出警告,这也适用于您。

这是帮助我的the Android bug report

我添加的proguard配置是:

-keepnames public class * extends android.support.v4.view.ActionProvider
-keepclassmembers public class * extends android.support.v4.view.ActionProvider {
    <init>(android.content.Context);
}