boski
boski

Reputation: 1146

IllegalStateException with TextView inside AlertDialog on Android project

I am working on Android project. I have some issue and I don't know how to fix it. I found similar topics, but suggested solutions didn't help.

After I click button to get Dialog (with EditText inside) everything work's fine. But when I go second time I got error message.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

my code

private void MakeDescription()
    {
        try
        {
            
            DialogInterface.OnClickListener ConfirmProductClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        String url = txtDesc.getText().toString(); 
                    // txtDesc it's a EditText
                        break;
                    case DialogInterface.BUTTON_NEUTRAL:
                        dialog.cancel();
                        break;
                    }
                }
            };
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(getResources().getString(R.string.app_label3));
            builder.setView(txtDesc).setPositiveButton(getResources().getString(R.string.app_labe1), // txtDesc it's a EditTextConfirmProductClickListener)
            .setNeutralButton(getResources().getString(R.string.app_label2), ConfirmProductClickListener)
            .show();

        }
        catch (Exception exc)
        {
            Log.e("ex", exc.toString());
        }
    }

Upvotes: 1

Views: 332

Answers (1)

FunkTheMonk
FunkTheMonk

Reputation: 10938

The exception is pretty specific, txtDesc is already in a View hierarchy - you can't reuse it if it is already in your activity, fragment, or other dialog.

One option is to make a new EditText view:

try
    {
        final EditText editText = new EditText(this);
        DialogInterface.OnClickListener ConfirmProductClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                case DialogInterface.BUTTON_POSITIVE:
                    String url = editText.getText().toString(); 
                    break;
                case DialogInterface.BUTTON_NEUTRAL:
                    dialog.cancel();
                    break;
                }
            }
        };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(getResources().getString(R.string.app_label3));
        builder.setView(editText).setPositiveButton(getResources().getString(R.string.app_labe1), // txtDesc it's a EditTextConfirmProductClickListener)
        .setNeutralButton(getResources().getString(R.string.app_label2), ConfirmProductClickListener)
        .show();

    }
    catch (Exception exc)
    {
        Log.e("ex", exc.toString());
    }

Upvotes: 2

Related Questions