Rickard
Rickard

Reputation: 1299

removeView() on the child's parent

I'm getting

E/AndroidRuntime(855): Caused by: java.lang.IllegalStateException: 
The specified child already has a parent. You must call removeView() 
on the child's parent first.

The code I'm running, the error happens on linearLayout.addView(view);

view = getFieldControl(field);
linearLayout.addView(view);

Where getFieldControl looks like this(simplified):

private android.view.View getFieldControl(ControlTemplate control)
{
    View view =null;    
    view = (EditText)findViewById(R.id.edit_message);
    ((EditText) view).setHint(control.getName());
    ((EditText) view).setText(control.getValue()); 

return view;
}

I don't understand what the views parent could be, where should I remove it from?

Upvotes: 0

Views: 189

Answers (3)

Sankar V
Sankar V

Reputation: 4863

Create your EditText programmatically as below

private android.widget.EditText getFieldControl(ControlTemplate control)
{
    EditText edittext = new EditText(this);
    edittext.setHint(control.getName());
    edittext.setText(control.getValue()); 
    return edittext;
}

Note : If the EditText is in XML which is set as Content View modify the code as below by removing the line linearLayout.addView(view); because already the EditText is added in the layout through XML.

EditText edittext = (EditText) findViewById(R.id.edit_message);;
getFieldControl(edittext, field);

private void getFieldControl(EditText edittext, ControlTemplate control)
{
    edittext.setHint(control.getName());
    edittext.setText(control.getValue()); 
}

Upvotes: 1

AnujMathur_07
AnujMathur_07

Reputation: 2596

Your EditText R.id.edit_message must be in an .xml file or say layout, that layout is the parent of the EditText.

Create a dynamic EditText instead.

Upvotes: 0

Kameswari
Kameswari

Reputation: 758

If you call the getFieldControl(field) method more than once, you are trying to get the EditText of R.id.edit_message from XML and adding that more than once to the layout. Hence it's giving this error. Make sure that you add this EditText only once to any of the layouts.

Upvotes: 1

Related Questions