调用startActivity时DialogFragment崩溃活动() [英] DialogFragment crashes Activity when calling startActivity()

查看:449
本文介绍了调用startActivity时DialogFragment崩溃活动()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DialogFragment 这应该是简单,但它是专门给我的果冻豆一些大问题。

应用程序使用网络,并且它会弹出一个对话框,询问用户打开WiFi或取消然后将其关闭。因此,它扩展了 DialogFragment 并创建视图:

  @覆盖
公共对话onCreateDialog(包savedInstanceState){

    AlertDialog A =新AlertDialog.Builder(getActivity())。setCancelable(真).setTitle(R.string.dialog_title_disabled)
            .setMessage(R.string.dialog_text)
            .setPositiveButton(android.R.string.yes,新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释whichButton){
                        解雇();
                        意图I =新的意图(Settings.ACTION_WIRELESS_SETTINGS);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(ⅰ);
                    }
            })。setNegativeButton(android.R.string.no,新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释whichButton){
                        getActivity()完成()。
                    }
            })。创建();
    //a.setCanceledOnTouchOutside(false);
    返回;
}
 

如果用户点击是,它驳回了对话,并打开无线设置活动。或者,如果用户点击取消,它只是关闭我的整个活动,但果冻豆,只要我单击是,它并打开设置,但应用程序力出现以下错误关闭:

  08-05 20:24:22.584:E / AndroidRuntime(2579):java.lang.IllegalStateException:失败节能状态:活跃SettingsDialogFragment {425dd550}已清除指数:-1
08-05 20:24:22.584:E / AndroidRuntime(2579):在android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1653)
 

有一些额外的记录显示每个片段在我的布局和2号本来应该是保存状态的 SettingsDialogFragment 只是一个

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

我想不排除对话,但它坠毁相同的方式。

我真的不知道是怎么回事......任何想法?


编辑:

该活动code(这是一种正常的活动,因为该应用程序的目标ICS及以上):

 私人无效的ShowDialog(){
    。SettingsDialogFragment诊断=(SettingsDialogFragment)getFragmentManager()findFragmentByTag(DIALOG_TAG);
    如果(诊断== NULL){
        诊断=新SettingsDialogFragment();
        diag.show(getFragmentManager(),DIALOG_TAG);
    } 其他 {
        如果(!diag.isVisible())
            diag.show(getFragmentManager(),DIALOG_TAG);
    }
}

私人无效dismissDialog(){
    。SettingsDialogFragment诊断=(SettingsDialogFragment)getFragmentManager()findFragmentByTag(DIALOG_TAG);
    如果(诊断!= NULL)
        diag.dismiss();
}
 

解决方案

显然,谷歌改变了从ICS东西JB并关闭对话,我不得不使用:

 解雇();
。getFragmentManager()的BeginTransaction()删除(FRAG).commit()。
 

看来,dialogFragment不是从片段经理除去自己OnDismiss像以前那样,如果有人在乎挖成的communitiy源 - code和仔细检查,这将是超级。

感谢。

I have a DialogFragment that was supposed to be simple but it's giving me some big problems specifically on Jelly Bean.

The app uses the network and it pops a dialogue asking the user to turn on WiFi or cancel then closes it. So it extends DialogFragment and creates view as:

@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:

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:

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):

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:

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.

thanks.

这篇关于调用startActivity时DialogFragment崩溃活动()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆