如何删除孩子父母的观点?安卓

use*_*402 6 android android-alertdialog

所以我有一个按钮,单击它时会显示一个警告对话框.我在活动的onCreate方法中为警报对话框创建视图.代码就在这里:

    LayoutInflater factory = LayoutInflater.from(this);
    view = factory.inflate(R.layout.grade_result, null);
Run Code Online (Sandbox Code Playgroud)

当我第一次按下按钮时,对话框显示我想要的方式,但是当我第二次按下它时会抛出此异常

11-28 00:35:58.066:E/AndroidRuntime(30348):引起:java.lang.IllegalStateException:指定的子节点已经有父节点.您必须首先在孩子的父母上调用removeView().

按下按钮时显示AlertDialog的方法的代码就在这里:

public void details(View v){
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setView(view);
    alert.setMessage("Details About Your Grades")
    .setCancelable(false)
    .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id){
            dialog.cancel();

        }
    });
    alert.show();
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激!谢谢!

小智 5

膨胀应该在AlertDialog的Builder中设置的视图对我有用:

Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // inflating view out of the onClick() method does not work
            LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final ViewGroup viewGroup= (ViewGroup) mInflater.inflate(R.layout.my_view, null);

            Dialog alertDialog = new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.my_title)
                    .setView(viewGroup)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).create();
            alertDialog.show();
        }
}
Run Code Online (Sandbox Code Playgroud)


Tal*_*lha -1

使用alert.create(); 然后alertd.dismiss(); 销毁对话框

 final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final AlertDialog alertd = alert.create();

        alert.setTitle("Title");
        alert.setMessage("Messaage");

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
              dialog.cancel();
              alertd.dismiss();
          }
        });
Run Code Online (Sandbox Code Playgroud)