Custom dialog - you must first call removeView () of the parent parent
I am using a custom AlertDialog for my project and when I try to show it a second time it tells me java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child parent first.
In onCreate
my activity I have:
infoDialog = new QustomDialogBuilder(this);
infoDialog.setTitle("Attenzione");
infoDialog.setTitleColor(Constants.ANTINORI_LIGHT);
infoDialog.setDividerColor(Constants.ANTINORI_LIGHT);
infoDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Later I use it as an AsyncTask answer:
//DO STUFF
infoDialog.setMessage(loginResponse.getMessage());
infoDialog.show();
The first time I show this infoDialog it works fine, but the second time it gives me IllegalStateException
.
I have read many oher posts on StackOverflow but none seem to be solving my problem. Hope someone can help me.
source to share
you can use this function below and then call this function when you want to show warning.
private void showDialog(String message) {
final Dialog dialog = new Dialog(CustomDialog.this);
dialog.setContentView(R.layout.custom_alert);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText(message);
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
and call this function like this: showDialog(loginResponse.getMessage())
source to share
I have the same problem, because of this I am setting the value of the view before generating the alert
Inflatable Fin Layout = Primera.this.getLayoutInflater ();
view = inflater.inflate(R.layout.dialog,null);
empezar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(Primera.this);
builder.setTitle(getResources().getString(R.string.dialog_codigo));
builder.setView(view);
builder.setPositiveButton(getResources().getString(R.string.dialog_aceptar), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
codigo = (EditText) view.findViewById(R.id.codigo);
ContentValues values = new ContentValues();
String valor;
valor = codigo.getText().toString();
values.put(Database.CODIGO_NOMBRE, valor);
mDbHelper.getWritableDatabase().insert(Database.TABLA_CODIGO, null, values);
if(codigo.getText().toString() == null || codigo.getText().toString().equals("")){
Toast.makeText( getApplicationContext(), "Codigo incorrecto" , Toast.LENGTH_SHORT ).show();
}
else
{
Toast.makeText( getApplicationContext(), "Codigo correcto" , Toast.LENGTH_SHORT ).show();
Intent intent = new Intent(Primera.this, Producto.class);
intent.putExtra("opcion",0);
intent.putExtra("primera",1);
startActivity(intent);
}
}
});
builder.setNegativeButton(getResources().getString(R.string.dialog_atras), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
dialog.cancel();
}
});
builder.create();
builder.show();
}
});
I set the value view inside the button and the problem was solved.
The code that worked:
empezar.setOnClickListener (new View.OnClickListener () {public void onClick (View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(Primera.this);
builder.setTitle(getResources().getString(R.string.dialog_codigo));
LayoutInflater inflater = Primera.this.getLayoutInflater();
view = inflater.inflate(R.layout.dialog,null);
builder.setView(view);
builder.setPositiveButton(getResources().getString(R.string.dialog_aceptar), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
codigo = (EditText) view.findViewById(R.id.codigo);
ContentValues values = new ContentValues();
String valor;
valor = codigo.getText().toString();
values.put(Database.CODIGO_NOMBRE, valor);
mDbHelper.getWritableDatabase().insert(Database.TABLA_CODIGO, null, values);
if(codigo.getText().toString() == null || codigo.getText().toString().equals("")){
Toast.makeText( getApplicationContext(), "Codigo incorrecto" , Toast.LENGTH_SHORT ).show();
}
else
{
Toast.makeText( getApplicationContext(), "Codigo correcto" , Toast.LENGTH_SHORT ).show();
Intent intent = new Intent(Primera.this, Producto.class);
intent.putExtra("opcion",0);
intent.putExtra("primera",1);
startActivity(intent);
}
}
});
builder.setNegativeButton(getResources().getString(R.string.dialog_atras), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
dialog.cancel();
}
});
builder.create();
builder.show();
}
});
source to share