First it runs smooth but when i click on it again it stops
I got error on trying to set dynamic rating bar view in AlertDialog
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
rtb = LayoutInflater.from(getContext()).inflate(R.layout.ratingbar_dialog, null);
rb = (RatingBar) rtb.findViewById(R.id.rtbDialog);
ad = new AlertDialog.Builder(getContext());
ad.setView(rtb);
ll1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ad.setMessage("Rate Price");
ad.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
ad.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
rtbPrice.setRating(rb.getRating());
}
});
ad.show();
}
});
Because each time you call ad.show()
, a new dialog will be created and the same view rtb
will be added as dialog content (So the 1st time works, then exception was raise in the 2nd time).
To fix it:
inflate
to setView
into the onClick
method onClick
, remove rtb
from it's parent: if (rtb.getParent() instanceOf ViewGroup) { ((ViewGroup) rtb.getParent()).removeView(rtb); }
Don't inflate the view
. . Set the xml via setContentView()
and access the rating bar
via FindviewbyId()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.