Android自定义Dialog Fragment崩溃

时间:2016-02-11 06:54:33

标签: android

我想显示Custom AlertDialog。

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

那么为什么Dialog没有显示,只是它连续调用onCreateDialog并且内存使用增长?

有效。直接在activiy上创建

@Override
    public void onClick(View v) {

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

}

它不起作用。创建自定义DialogClass并在活动上调用它

//呼叫

    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

以下是我的日志:

NewtonSoft.Json

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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