Reputation: 833
I'm using a custom AlertDialog for my project and, when i try to show it the second time it tells me java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
In onCreate
of 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 reply of an AsyncTask:
//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 the IllegalStateException
.
I've read a lot of oher post on StackOverflow, but no one seems to solve my problem. Hope someone can help me.
Upvotes: 3
Views: 4649
Reputation: 59
I have the same problem, because of this i set the view value before alertlog is created
LayoutInflater inflater = 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 value view inside of the button, and problem solved.
Its worked code:
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();
}
});
Upvotes: 0
Reputation: 2240
you can use the function below and then call this function when you want to show alert.
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())
Upvotes: 1