gpt4 book ai didi

android - 显示 DialogFragments 会使 ICS 崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:55 26 4
gpt4 key购买 nike

Fragments API 发布后,我开始使用兼容包将所有已弃用的对话框移植到 DialogFraments 中。一切都运行良好,直到我注意到我的对话框仅导致 ICS 崩溃:

E/AndroidRuntime(  883): FATAL EXCEPTION: main
E/AndroidRuntime( 883): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
E/AndroidRuntime( 883): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1254)
E/AndroidRuntime( 883): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1265)
E/AndroidRuntime( 883): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
E/AndroidRuntime( 883): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525)
E/AndroidRuntime( 883): at android.support.v4.app.DialogFragment.show(DialogFragment.java:123)
E/AndroidRuntime( 883): at com.myapp.ui.dialogs.TwoButtonDialogFragment.showDialog(TwoButtonDialogFragment.java:84)

我的对话框显示在 AsyncTask.onPostExecute() 上,以便向用户显示 http 响应。深入研究问题后得出的结论是,这个异常只有在Activity暂停或停止时才会发生,其他版本的Android不会发生。我试过使用 commitAllowingStateLoss(),但它没有帮助,因为在 DialogFragment.show() 上抛出了异常。这是我的 DialogFragment 代码:

private static void showDialog(FragmentActivity activity, String title, String msg, 
String positiveButtonText, String negativeButtonText, int id, Bundle args) {

if (activity.isFinishing()) {
return;
}

FragmentManager fmgr = activity.getSupportFragmentManager();
FragmentTransaction ft = fmgr.beginTransaction();
Fragment prev = fmgr.findFragmentByTag(TAG);
if (prev != null) {
try {
ft.remove(prev);
} catch (IllegalStateException ex) {
// issue: http://code.google.com/p/android/issues/detail?id=17029
}
}

TwoButtonDialogFragment newFragment = new TwoButtonDialogFragment();
if (args == null) {
args = new Bundle();
}
args.putString("title", title);
args.putString("message", msg);
args.putString("positiveButtonText", positiveButtonText);
args.putString("negativeButtonText", negativeButtonText);
args.putInt("id", id);
newFragment.setArguments(args);
newFragment.setCancelable(false);
newFragment.show(fmgr, TAG); // exception is thrown here
ft.commit();
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Bundle args = getArguments();
String title = args.getString("title");
String msg = args.getString("message");
String positiveButtonText = args.getString("positiveButtonText");
String negativeButtonText = args.getString("negativeButtonText");
final int id = args.getInt("id");

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
builder.setMessage(msg);

final TwoButtonDialogHandler handler = (TwoButtonDialogHandler) getActivity();
builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
handler.doPositiveClick(id, args);
}
});
builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
handler.doNegativeClick(id, args);
}
});

return builder.create();
}

这是 ICS 上的错误吗?我该怎么办?

最佳答案

我遇到了这个问题,但在框架中找不到解决这个问题的方法。

但是我确实提供了解决该问题的方法,您可以在下面的 link 中看到该问题

关于android - 显示 DialogFragments 会使 ICS 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8520561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com