当我在显示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)
那是我的DialogFragment:
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> {
...
}
}
错误说&#34;在onSaveInstanceState&#34;之后无法执行此操作。但我不知道InstanceState的保存位置。对我来说,可以在旋转后关闭对话框并重新创建它。
知道出了什么问题吗?
编辑:
我的DialogFragment在主要活动中创建:
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的很多人都说你应该在onCreateDialog
中使用onCreateView
或DialogFragment
。也许这就是我的问题。但是,如果我删除onCreateDialog
和dialog
变量,则解雇不再有效。到目前为止,我使用此解决方案: Cannot get DialogFragment to dismiss programatically(仅用于解除对话框,但在轮换时崩溃)
答案 0 :(得分:1)
我认为问题是onDestroy在父活动上调用Fragment事务提交,并且在onSavedInstance被调用并且在onDestroy之前完成之后无法完成。你真的需要解雇onDestroy()??
答案 1 :(得分:1)
您获得泄漏窗口异常,因为在您的活动被销毁时function MakeVer(Major, Minor, Build: Cardinal): Cardinal;
begin
Result := (Major shl 24) + (Minor shl 16) + Build;
end;
function InitializeSetup(): Boolean;
var
WindowsVersion: Cardinal;
begin
Result := True;
WindowsVersion := GetWindowsVersion;
Log(Format('Windows Version: %x', [WindowsVersion]));
if (WindowsVersion < MakeVer(6, 1, 7601)) or
((WindowsVersion >= MakeVer(6, 2, 0)) and (WindowsVersion < MakeVer(6, 3, 0))) then
begin
MsgBox(SetupMessage(msgWindowsVersionNotSupported), mbError, MB_OK);
Result := False;
end;
end;
正在运行。你应该在活动被销毁时解雇对话
试试这个:
Alert dialog
同样在@Override
protected void onDestroy() {
try {
if (dialog!= null && dialog.isShowing()) {
dialog.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
super.onDestroy();
}
首先解除对话
setOnItemClickListener
答案 2 :(得分:0)
与此同时,我尝试过很多东西。我将ActionsDialogFragment
更改为更像this tutorial。
但我认为真正的错误出现在我的自定义ListAdapter
中。我改变了这部分
public @NonNull View getView(int position, View view, @NonNull ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater(null);
到
public @NonNull View getView(int position, View view, @NonNull ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
现在我可以通过一个简单的DialogFragment
来解除dismiss();
,并且该应用在轮播后不会再崩溃。
所以我应该在我的问题中发布完整的课程。无论如何,谢谢你的帮助!