0

我正在创建用于查看图像的应用程序。所以,我决定在网格视图中实现图片的多选删除。我正在使用 setMultiChoiceModeListener 方法。一切正常,但我有一个问题......我无法在 onDestroyActionMode 中关闭上下文菜单方法。我总是遇到这样的异常:

 java.lang.StackOverflowError
        at android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onDestroyActionMode(SupportActionModeWrapper.java:174)
        at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onDestroyActionMode(AppCompatDelegateImplV7.java:1656)
        at android.support.v7.internal.app.WindowDecorActionBar$ActionModeImpl.finish(WindowDecorActionBar.java:972)
        at android.support.v7.internal.view.SupportActionModeWrapper.finish(SupportActionModeWrapper.java:78)
        at com.example.vasylpaliy.mediaview.ImageList$1.onDestroyActionMode(ImageList.java:91)
        at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6604)
        at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:3048)
        at android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onDestroyActionMode(SupportActionModeWrapper.java:174)
        at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onDestroyActionMode(AppCompatDelegateImplV7.java:1656)
        at android.support.v7.internal.app.WindowDecorActionBar$ActionModeImpl.finish(WindowDecorActionBar.java:972)
        at android.support.v7.internal.view.SupportActionModeWrapper.finish(SupportActionModeWrapper.java:78)
        at com.example.vasylpaliy.mediaview.ImageList$1.onDestroyActionMode(ImageList.java:91)
        at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6604)
        at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:3048)
        at android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onDestroyActionMode(SupportActionModeWrapper.java:174)
        at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onDestroyActionMode(AppCompatDelegateImplV7.java:1656)
        at android.support.v7.internal.app.WindowDecorActionBar$ActionModeImpl.finish(WindowDecorActionBar.java:972)
        at android.support.v7.internal.view.SupportActionModeWrapper.finish(SupportActionModeWrapper.java:78)
        at com.example.vasylpaliy.mediaview.ImageList$1.onDestroyActionMode(ImageList.java:91)
        at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6604)
        at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:3048)
        at android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onDestroyActionMode(SupportActionModeWrapper.java:174)
        at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onDestroyActionMode(AppCompatDelegateImplV7.java:1656)
        at android.support.v7.internal.app.WindowDecorActionBar$ActionModeImpl.finish(WindowDecorActionBar.java:972)
        at android.support.v7.internal.view.SupportActionModeWrapper.finish(SupportActionModeWrapper.java:78)
        at com.example.vasylpaliy.mediaview.ImageList$1.onDestroyActionMode(ImageList.java:91)
        at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6604)
        at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:3048)
        at android.support.v7.internal.view.SupportActionModeWrapper$CallbackWrapper.onDestroyActionMode(SupportActionModeWrapper.java:174)
        at android.support.v7.app.AppCompatDelegateImplV7$ActionModeCallbackWrapperV7.onDestroyActionMode(AppCompatDelegateImplV7.java:1656)
        at android.support.v7.internal.app.WindowDecorActionBar$ActionModeImpl.finish(WindowDecorActionBar.java:972)
        at android.support.v7.internal.view.SupportActionModeWrapper.finish(SupportActionModeWrapper.java:78)
        at com.example.vasylpaliy.mediaview.ImageList$1.onDestroyActionMode(ImageList.java:91)
        at android.widget.AbsListView$MultiChoiceModeWrapper.onDestroyActionMode(AbsListView.java:6604)
        at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onDestroyActionMode(PhoneWindow.java:3048)

这是代码:

view.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        private boolean isDeleted;
        private ArrayList<String> imageItems;
        {
            imageItems=new ArrayList<String>();
            isDeleted=false;
        }
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            imageItems.add(imagePaths.get(position));
            mode.setTitle("Selected:" + Integer.toString(view.getCheckedItemCount()));
            imageAdapter.setCheckMark(position);
            imageAdapter.notifyDataSetChanged();
        }
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.multiple_audio, menu);
            return true;
        }
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId())
            {
                case R.id.multiple_audio_select_all:
                {
                    imageItems=imagePaths;
                    for(int i=0;i<imageItems.size();i++)
                        view.setItemChecked(i,true);
                    break;
                }
                case R.id.multiple_audio_deleting:
                {
                    deleteItems(imageItems);
                    isDeleted=true;
                    onDestroyActionMode(mode);
                    break;
                }
            }
            return true;
        }
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            if(mode!=null)
                mode.finish();
        }
    });
4

2 回答 2

0

finish()将导致调用onDestroyActionMode()它不会将您的模式设置为空。

所以你只需要:

@Override
public void onDestroyActionMode(ActionMode mode) {
     // nothing here ;)
}

另请注意,如果您想在此处完成操作模式:

 case R.id.multiple_audio_deleting:
     {
     deleteItems(imageItems);
     isDeleted=true;
     onDestroyActionMode(mode);
     break;
 }

将其更改为:

case R.id.multiple_audio_deleting:
     {
     deleteItems(imageItems);
     isDeleted=true;
     mode.finish();
     break;
 }
于 2015-11-27T13:29:39.267 回答
0

我认为您的 StackoverflowError 是由您执行“onActionItemClicked()”和“onDestroyActionMode()”引起的:

每次执行mode.finish();“onDestroyActionMode()”时,框架都会再次精确调用此方法,因此您正在创建一种无限循环。

根据官方文档中的示例,尤其是关于在 ListView 或 GridView 中启用批处理上下文操作的部分,您应该更改两件事:

1.在您的“onActionItemClicked()”实现中,调用

mode.finish();

而不是“onDestroyActionMode()”。

2.在你的“onDestroyActionMode()”中,你不需要调用“finish()”。您可以覆盖此方法以

...在删除 CAB 时对活动进行任何必要的更新。默认情况下,选定的项目被取消选择/取消选中。

于 2015-11-27T13:30:14.217 回答