Android自定义对话框片段崩溃了。

4
我想展示自定义的AlertDialog。
因此,我创建了一个自定义对话框并在每次需要时调用它,只需简单地调用即可。但是,当我从DialogFragment扩展CustomDialog时,它会崩溃,我进行了调试,并显示onCreateDialog方法被调用多次。这会使用设备上的所有内存。但是当我仅在活动中创建警报对话框时,它可以正常工作。
因此,为什么对话框没有显示,仅仅是不断调用onCreateDialog并增加内存使用量呢? 它可以正常工作,直接在活动中创建。
@Override
public void onClick(View v) {

    LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
    View promptsView = layoutInflater.inflate(R.layout.fragment_additional_info, null);

    LinearLayout containerLinear = (LinearLayout) promptsView.findViewById(R.id.linearLayoutLoadingProgressbarContainer);
    Button buttonOk = (Button) promptsView.findViewById(R.id.buttonOk);

    for (KeyValueString keyValueItem : info.getAdditionalInfo().getKeyValueString()) {
        if (!keyValueItem.getKey().equals("fee")) {
            View viewToAdd = layoutInflater.inflate(R.layout.template_key_value, null);

            TextView tvKey = (TextView) viewToAdd.findViewWithTag("key");
            tvKey.setText(keyValueItem.getKey());
            TextView tvValue = (TextView) viewToAdd.findViewWithTag("value");
            tvValue.setText(keyValueItem.getValue());

            containerLinear.addView(viewToAdd);
        }
    }


    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(GuestPayActivity.this);
    alertDialogBuilder.setView(promptsView);
    alertDialogBuilder.setNegativeButton(R.string.button_cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    final AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

它没有起作用。创建自定义DialogClass并在活动中调用它

//调用

@Override
    public void onClick(View v) {

        DialogFragment newFragment = AdditionalInfoFragment.newInstance(info.getAdditionalInfo().getKeyValueString());
        newFragment.show(getSupportFragmentManager(), "dialog");

}

//自定义对话框类

    public class AdditionalInfoFragment extends DialogFragment {
    private static final String TAG = "AdditionalInfoFragment";
    List<KeyValueString> mAdditionalInfo;

    public static AdditionalInfoFragment newInstance(List<KeyValueString> additionalInfo) {
        AdditionalInfoFragment f = new AdditionalInfoFragment();

        Bundle args = new Bundle();
        args.putSerializable("add_info", (Serializable) additionalInfo);
        f.setArguments(args);

        return f;
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdditionalInfo = (List<KeyValueString>) getArguments().getSerializable("add_info");

        // int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme_Black_NoTitleBar_Fullscreen;
        // setStyle(style, theme);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.fragment_additional_info, null);

   LinearLayout containerLinear = (LinearLayout) v.findViewById(R.id.linearLayoutLoadingProgressbarContainer);
        Button buttonOk = (Button) v.findViewById(R.id.buttonOk);

        for (KeyValueString keyValueItem : mAdditionalInfo) {
            if (!keyValueItem.getKey().equals("fee")) {
                View viewToAdd = getLayoutInflater(savedInstanceState).inflate(R.layout.template_key_value, null);

                TextView tvKey = (TextView) viewToAdd.findViewWithTag("key");
                tvKey.setText(keyValueItem.getKey());
                TextView tvValue = (TextView) viewToAdd.findViewWithTag("value");
                tvValue.setText(keyValueItem.getValue());

                containerLinear.addView(viewToAdd);
            }
        }



        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setIcon(R.drawable.ic_bonuses)
                .setView(v)
                .setTitle(R.string.message_authenticating)
                .setPositiveButton(R.string.button_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                dismiss();
                            }
                        }
                );




        return builder.create();
    }

这是我的日志记录:

02-11 10:58:58.429 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 17975(561KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 52MB/53MB, paused 5.415ms total 95.850ms
02-11 10:58:58.652 24794-24800/test.app W/art: Suspending all threads took: 74.554ms
02-11 10:58:58.675 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 3160(137KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 53MB/69MB, paused 5.378ms total 153.858ms
02-11 10:59:01.135 24794-24800/test.app W/art: Suspending all threads took: 52.945ms
02-11 10:59:01.139 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 17897(559KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 68MB/69MB, paused 7.785ms total 107.723ms
02-11 10:59:01.446 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 3859(155KB) AllocSpace objects, 0(0B) LOS objects, 18% free, 70MB/86MB, paused 9.483ms total 266.572ms
02-11 10:59:03.868 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 18212(569KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 86MB/86MB, paused 10.623ms total 149.134ms
02-11 10:59:04.166 24794-24800/test.app W/art: Suspending all threads took: 77.074ms
02-11 10:59:04.190 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 3129(123KB) AllocSpace objects, 0(0B) LOS objects, 15% free, 87MB/103MB, paused 10.948ms total 289.089ms
02-11 10:59:06.669 24794-24800/test.app W/art: Suspending all threads took: 9.851ms
02-11 10:59:06.702 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 18118(566KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 102MB/103MB, paused 95.894ms total 245.108ms
02-11 10:59:07.148 24794-24800/test.app W/art: Suspending all threads took: 52.525ms
02-11 10:59:07.157 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 5396(215KB) AllocSpace objects, 0(0B) LOS objects, 13% free, 104MB/120MB, paused 14.787ms total 433.896ms
02-11 10:59:09.540 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 17494(546KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 120MB/120MB, paused 23.833ms total 138.768ms
02-11 10:59:10.245 24794-24800/test.app W/art: Suspending all threads took: 144.977ms
02-11 10:59:10.283 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 5438(206KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 122MB/138MB, paused 17.375ms total 577.503ms
02-11 10:59:12.782 24794-24800/test.app W/art: Suspending all threads took: 176.769ms
02-11 10:59:12.818 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 17243(539KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 137MB/138MB, paused 20.240ms total 213.495ms
02-11 10:59:13.605 24794-24800/test.app W/art: Suspending all threads took: 500.021ms
02-11 10:59:13.616 24794-24804/test.app I/art: Background partial concurrent mark sweep GC freed 3830(157KB) AllocSpace objects, 0(0B) LOS objects, 10% free, 138MB/154MB, paused 19.801ms total 608.081ms
02-11 10:59:16.121 24794-24800/test.app W/art: Suspending all threads took: 8.391ms
02-11 10:59:16.161 24794-24804/test.app I/art: Background sticky concurrent mark sweep GC freed 18088(565KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 154MB/154MB, paused 21.944ms total 176.228ms
02-11 10:59:16.807 24794-24800/test.app W/art: Suspending all threads took: 194.938ms
02-11 10:59:16.821 24794-24804/test.app W/art: Suspending all threads took: 12.390ms

3
请分享崩溃日志。 - ρяσѕρєя K
可能是 mAdditionalInfo = (List<KeyValueString>) getArguments().getSerializable("add_info"); 这一行代码引起了问题,请分享应用程序的崩溃日志而不是整个设备的。 - ρяσѕρєя K
1
那不像是一个崩溃日志。另外,当粘贴日志时,请将其格式化为代码以便更容易阅读。 - Doug Stevenson
抱歉,我无法向您发送任何崩溃信息,有些东西出了问题,只显示这些日志,并且在内存选项卡上,我看到使用的内存正在增长。 - AEMLoviji
1
我将创建一个简单的应用程序来说明这个问题。谢谢。 - AEMLoviji
显示剩余4条评论
2个回答

2
我刚刚对onCreateDialog方法进行了一些更改。
这些行被替换了:
1.old : LayoutInflater inflater = getActivity().getLayoutInflater(); 
1.new : enter code hereLayoutInflater layoutInflater = LayoutInflater.from(this.getActivity());

2.old : View viewToAdd = getLayoutInflater(savedInstanceState).inflate(R.layout.template_key_value, null);
2.new : View viewToAdd = layoutInflater.inflate(R.layout.template_key_value, null);

问题出在第二次替换上。当你调用getLayoutInflater()时,它会试图一遍又一遍地调用onCreateDialog。因此,只需使用第一个创建的inflater对象(layoutInflater)即可。

我的新代码如下:

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater layoutInflater = LayoutInflater.from(this.getActivity());
    View promptsView = layoutInflater.inflate(R.layout.fragment_additional_info, null);

    LinearLayout containerLinear = (LinearLayout) promptsView.findViewById(R.id.linearLayoutLoadingProgressbarContainer);

    for (KeyValueString keyValueItem : mAdditionalInfo) {
        if (!keyValueItem.getKey().equals("fee")) {
            View viewToAdd = layoutInflater.inflate(R.layout.template_key_value, null);

            TextView tvKey = (TextView) viewToAdd.findViewWithTag("key");
            tvKey.setText(keyValueItem.getKey());
            TextView tvValue = (TextView) viewToAdd.findViewWithTag("value");
            tvValue.setText(keyValueItem.getValue());

            containerLinear.addView(viewToAdd);
        }
    }


    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setIcon(R.drawable.ic_bonuses)
            .setTitle(R.string.title_additional_info)
            .setView(promptsView)
            .setNegativeButton(R.string.button_close, null);


    return builder.create();
}

0

请按照以下方式初始化您的对话片段:

AdditionalInfoFragment dialog = new AdditionalInfoFragment();

将您的对话框片段类更改为以下内容

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_additional_info, null);
 return v;
}

它会改变什么? - AEMLoviji
它会给出这样的错误。android.util.AndroidRuntimeException: 必须在添加内容之前请求窗口特性。 - AEMLoviji

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接