当我在显示DialogFragment时旋转设备时,我的应用崩溃:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 27909
java.lang.RuntimeException: Unable to destroy activity {com.myapp/com.myapp.FileListActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4203)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4221)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4495)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:201)
at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:167)
at com.myapp.ActionsDialogFragment.onDestroy(ActionsDialogFragment.java:127)
at android.support.v4.app.Fragment.performDestroy(Fragment.java:2202)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1196)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:2083)
at android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:244)
at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:368)
at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:203)
at com.myapp.FileListActivity.onDestroy(FileListActivity.java:1106)
at android.app.Activity.performDestroy(Activity.java:6881)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1154)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4190)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4221)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4495)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是我的对话片段:
public class ActionsDialogFragment extends DialogFragment {
public interface ActionsDialogListener {
void onActionSelected(DialogFragment dialog, int position);
}
AlertDialog dialog;
ActionsDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (ActionsDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ActionsDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
dialog = builder.create();
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.actions_dialog, container, false);
CustomListAdapter cla;
cla = new CustomListAdapter(getActivity());
ListView list = (ListView) view.findViewById(R.id.listViewActions);
final AlertDialog finalDialog = dialog;
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mListener.onActionSelected((DialogFragment) getParentFragment(), position);
Log.d(FileListActivity.TAG, "Dismissing dialog");
// ActionsDialogFragment.this.dismiss();
finalDialog.dismiss();
}
});
list.setAdapter(cla);
dialog.setView(view);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(FileListActivity.TAG, "ActionDialogFragment onDestroy()");
dismiss();
// dialog.dismiss();
}
class CustomListAdapter extends ArrayAdapter<String> {
...
}
}
错误显示“在onSaveInstanceState之后无法执行此操作”。但我不知道InstanceState保存在哪里。对我来说,关闭对话框并在旋转后重新创建它是可以的。
知道出什么问题了吗?
编辑:
My DialogFragment在主Activity中创建,如下所示:
void showActionsDialog() {
actionsDialog = new ActionsDialogFragment();
actionsDialog.show(getSupportFragmentManager(), "ActionsDialogFragment");
}
编辑2:
以防万一,这是我的主要活动的onDestroy()
方法:
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "FileListActivity onDestroy()");
if (exifReadingDialog != null) { exifReadingDialog.dismiss(); }
dataFragment.mRetainedCache = mMemoryCache;
dataFragment.setData(mediaFolder);
}
编辑3:
一些额外的想法:stackoverflow的很多人说你应该在DialogFragment
中使用onCreateDialog
或者onCreateView
,也许这就是我的问题,但是如果我删除onCreateDialog
和dialog
变量,dismissing就不再起作用了,到目前为止我使用的是这个解决方案:Cannot get DialogFragment to dismiss programatically(仅用于关闭对话框,但在旋转时崩溃)
4条答案
按热度按时间ss2ws0br1#
我认为问题在于onDestroy调用父Activity上的Fragment事务提交,而这在调用onSavedInstance之后无法完成,并且在onDestroy之前完成。您真的需要关闭onDestroy()吗?
dphi5xsq2#
您得到泄漏窗口异常,因为
Alert dialog
正在运行,而您的活动被销毁。您应该在活动被销毁时关闭对话框试试这个:
同样,在
setOnItemClickListener
中,请先关闭对话框ds97pgxw3#
同时我尝试了很多方法,我改变了我的
ActionsDialogFragment
,让它看起来更像this tutorial。但我认为真实的的错误是在我的自定义
ListAdapter
中,我更改了这一部分到
现在,我可以通过一个简单的
dismiss();
来解除DialogFragment
,并且应用程序在旋转后不再崩溃。所以我应该在我的问题中贴出完整的类。无论如何谢谢你的帮助!
hwazgwia4#
我也遇到了同样的问题。不要在onDestroy()中忽略它。在onPause()中执行它。我希望这能有所帮助!