How to remove view on the child's parent? andr

2019-04-08 08:32发布

So i have a button that displays an alert dialog when its clicked. I create the view for the alert dialog in the onCreate method of my activity. The code for that is right here:

    LayoutInflater factory = LayoutInflater.from(this);
    view = factory.inflate(R.layout.grade_result, null);

When i push the button for the first time, the dialog displays the way i want it, but when i push it a second time it throws this exception

11-28 00:35:58.066: E/AndroidRuntime(30348): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

My code for the method that displays the AlertDialog when the button is pushed is right here:

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();

Any help would be appreciated! Thank you!

7条回答
闹够了就滚
2楼-- · 2019-04-08 09:10

Use this code

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(view);
alert.setMessage("Details About Your Grades")
.setCancelable(false)
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) 
    {
        dialog.dismiss();
    }
});
AlertDialog alertdialog = alert.create();
alertdialog .show()
查看更多
登录 后发表回答