我正在创建用于查看图像的应用程序。所以,我决定在网格视图中实现图片的多选删除。我正在使用 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();
}
});