可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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!
回答1:
Inflating the view that should be set within the Builder of the AlertDialog worked for me :
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();
}
}
回答2:
I had similar issue with alert dialog. First time I inflated a view and second time I wanted to display a message.
If you display the alert dialog once then you inflate the view and the dialog is cached. If you display it second time you get the error IllegalStateException:"call removeView() on child's parent first"
I solved this issue by removing the view that I inflated on first click and displayed the message on second click.
-remove the view or message every time before you inflate or create a message.
alertDialogBuilder.setView(null).setMessage(null);
alertDialogBuilder.setView(yourView).setMessage(yourMessage);
I modified Your code and it work's fine with me.
LayoutInflater factory = LayoutInflater.from(this);
mView = factory.inflate(R.layout.grade_result, null);
public void details(View v){
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(null).setMessage(null);
alert.setView(mView);
alert.setMessage("Details About Your Grades")
.setCancelable(false)
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
alert.show(); }
回答3:
Use this :
final AlertDialog.Builder alert = new AlertDialog.Builder(ProductsListAfterFilter.this);
alert.setTitle("Добавить продукт в корзину?");
alert.setMessage("Введите количество: ");
// Set an EditText view to get user input
final EditText input = new EditText(ProductsListAfterFilter.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int value = 0 ;
try {
value = Integer.valueOf(input.getText().toString());
}
catch (Exception err){
Toast.makeText(getApplicationContext(), "Введите число", 100).show();
}
//insertValue(value,st1,st2,st3,st4);
Toast.makeText(getApplicationContext(), "value = "+value, 100).show();
dialog.dismiss();
dialog.cancel();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.cancel();
dialog.dismiss();
}
});
final AlertDialog alertd = alert.create();
alertd.show();
回答4:
It says that your "view" was connected to parent. It's happend when you called .setView(view). If i'm right - alertDialog class gives parent for your view. It is basics for android. ViewGroup may have a lot of child View's. I had similar problem.
So, you can try this insinde onClick:
ViewGroup vGroup = view.getParent();
vGroup.removeView(view);
// And right after that - close dialog
dialog.dismiss();
It helped me, I hope it'll help you!
回答5:
Use alert.create(); then alertd.dismiss(); to destroy dialog
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();
}
});
回答6:
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()
回答7:
you can set View to null before setting the view in the dialog.
just like this:
alert.setView(null);
alert.setView(view);
It will always set view to null before setting it again.