Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specified child already has a parent error on inflated view

Immediate help needed!! The code used to work fine, but after a while I noticed rating doesnt work anymore and gives "the specified child already has a parent, call removeView() first..." on thealertDialog.show();` line:

///// Rating bar initialising
    Context mContext = ShowActivity.this;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    layout = inflater.inflate(R.layout.custom_rate_dialog, (ViewGroup) findViewById(R.id.layout_root));

    final RatingBar rating_indicator = (RatingBar)findViewById(R.id.rating_bar_cafe_indicator);
    final float current_rating = (float)mDbHelper.getInt(mRowId, type, RATE_COLUMN);
    rating_indicator.setRating(current_rating);


    rateDialogBuilder = new AlertDialog.Builder(ShowActivity.this);
    rateDialogBuilder.setView(layout);
    rateDialogBuilder.setCancelable(false);
    rateDialogBuilder.setIcon(R.drawable.rate_star_med_on);
    rateDialogBuilder.setTitle("RATE IT!");
    rateDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            rating_indicator.setRating(mDbHelper.getInt(mRowId, type, RATE_COLUMN));
            ((ViewGroup)layout.getParent()).removeView(layout);
            dialog.cancel();
        }
    });

    ratingText = (TextView) layout.findViewById(R.id.rate_text);
    rating = (RatingBar) layout.findViewById(R.id.rating_bar_cafe);
    rating.setRating(current_rating);
    rating.setStepSize(1);
    rating.setOnRatingBarChangeListener(ShowActivity.this);

    if ((int)current_rating != 0) ratingText.setText("Your rating: " + (int)current_rating); 

    Button rateButton = (Button) findViewById(R.id.button_rate); 
    rateButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            AlertDialog alertDialog = rateDialogBuilder.create(); 
            alertDialog.show();

        }
    });

    //////

Strange thing, if I call removeAllViews() or removeView() just before alertDialog.show(); it does not crash but opens messed up dialogue where I can see both the rating and the current activity or smth. Also the problem might be somewhere else in the code because this used to work fine, any ideas what should I look for or how to change this to make it work?

like image 838
user975869 Avatar asked Dec 21 '22 05:12

user975869


1 Answers

The error you're getting is that you are adding a View to a parent after it has already been added to a parent.

Looking at your code, it looks like,

layout = inflater.inflate(R.layout.custom_rate_dialog, (ViewGroup) findViewById(R.id.layout_root));

adds custom_rate_dialog to you layout_root. Then, you're adding it to another parent when you do rateDialogBuilder.setView(layout);

If you're just trying to inflate that layout to set it as the View for the Dialog then just do

layout = inflater.inflate(R.layout.custom_rate_dialog, null);
like image 85
LuxuryMode Avatar answered Dec 27 '22 01:12

LuxuryMode