Android 自定义 Dialog Fragment 崩溃

标签 android

我想显示自定义 AlertDialog。

所以我正在创建自定义对话框并每次都调用它,只是简单地调用它。 当我创建从 DialogFragment 扩展的 CustomDialog 时它崩溃了,我调试它并显示 onCreateDialog 方法调用多次。它使用了我设备上的所有内存。但是,当我只是在我的 Activity 中创建警报对话框时,它就起作用了。

那么为什么 Dialog 不显示,只是不断调用 onCreateDialog 并且内存使用量增加?

它有效。直接在activiy上创建

@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 并在 Activity 中调用它

//调用

@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

最佳答案

我刚刚对 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();
}

关于Android 自定义 Dialog Fragment 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35332664/

相关文章:

android - 为什么总是 LocationSettingsStatusCodes.SUCCESS 即使 GPS 关闭

android - 在应用程序安装期间获取针对 API 26 的所有权限

android - Fragment 的 UI 性能

android - 为传入的 SMS 注册 BroadcastReceiver

android - 从 Android 控制第三方 Cast session ?

java - 使用快速搜索框进行动态 Android 搜索

android - 如何知道用户在 android 的 youtube api v2 中接受 oauth 权限

android - 安卓中的weka?

java - 尝试使用 HttpClient 获取 DOMINO HTTP session — 响应代码始终为 200

Android CMake 工具链错误?