简体   繁体   English

调用startActivity()时DialogFragment崩溃活动

[英]DialogFragment crashes Activity when calling startActivity()

I have a DialogFragment that was supposed to be simple but it's giving me some big problems specifically on Jelly Bean. 我有一个DialogFragment简单的DialogFragment ,但它给了我一些特别关于Jelly Bean的大问题。

The app uses the network and it pops a dialogue asking the user to turn on WiFi or cancel then closes it. 该应用程序使用网络,它弹出一个对话框,要求用户打开WiFi或取消然后关闭它。 So it extends DialogFragment and creates view as: 因此它扩展了DialogFragment并创建视图:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog a = new AlertDialog.Builder(getActivity()).setCancelable(true).setTitle(R.string.dialog_title_disabled)
            .setMessage(R.string.dialog_text)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dismiss();
                        Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(i);
                    }
            }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        getActivity().finish();
                    }
            }).create();
    //a.setCanceledOnTouchOutside(false);
    return a;
}

If the user clicks Yes, it dismisses the dialogue and opens the Wireless settings activity. 如果用户单击“是”,则会取消对话框并打开“无线设置”活动。 Or if the user clicks Cancel it just closes my whole activity, but on Jelly Bean, anytime I click Yes, it does open the Settings, but the app force closes with the following error: 或者,如果用户单击“取消”,它只会关闭我的整个活动,但是在Jelly Bean上,只要我单击“是”,它就会打开“设置”,但应用程序强制将关闭并显示以下错误:

08-05 20:24:22.584: E/AndroidRuntime(2579): java.lang.IllegalStateException: Failure saving state: active SettingsDialogFragment{425dd550} has cleared index: -1
08-05 20:24:22.584: E/AndroidRuntime(2579):     at android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1653)

There's some extra logging showing the saved state of each fragment that was on my layout and the number 2 that was supposed to be the SettingsDialogFragment is just a null : 还有一些额外的日志记录显示了我布局中每个片段的保存状态,而应该是SettingsDialogFragment的数字2只是一个null

08-05 20:24:22.576: E/FragmentManager(2579):     #2: null

I tried to not dismiss the dialogue but it crashed the same way. 我试图不解除对话,但它以同样的方式崩溃。

I'm really not sure what's going on here… Any ideas? 我真的不确定这里发生了什么......有什么想法吗?


EDIT: 编辑:

The Activity code (it's a normal activity because the app targets ICS and up): 活动代码(这是一个正常的活动,因为该应用程序的目标是ICS及以上):

 private void showDialog() { SettingsDialogFragment diag = (SettingsDialogFragment) getFragmentManager().findFragmentByTag(DIALOG_TAG); if (diag == null) { diag = new SettingsDialogFragment(); diag.show(getFragmentManager(), DIALOG_TAG); } else { if (!diag.isVisible()) diag.show(getFragmentManager(), DIALOG_TAG); } } private void dismissDialog() { SettingsDialogFragment diag = (SettingsDialogFragment) getFragmentManager().findFragmentByTag(DIALOG_TAG); if (diag != null) diag.dismiss(); } 

Apparently Google changed something from ICS to JB and to dismiss the dialogue I had to use: 显然谷歌改变了从ICS到JB的东西,并驳回了我必须使用的对话:

dismiss();
getFragmentManager().beginTransaction().remove(frag).commit();

It seems that the dialogFragment is not removing itself from the fragment manager OnDismiss like it used to, if someone cares to dig into the source-code and double check for the communitiy, it would be super. 似乎dialogFragment并没有像以前那样从片段管理器OnDismiss中删除它自己,如果有人想要深入挖掘源代码并仔细检查社区,它就会超级。

thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM