Android

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

tomato13 2013. 7. 23. 17:45

TextView l_tv = new TextView(this);

l_tv.setText("you can do it");

l_tv.setTextColor(Color.WHITE);

l_tv.setBackgroundColor(Color.BLUE);


TextView l_tv2 = new TextView(this);

l_tv2.setText("hello");

l_tv2.setTextColor(Color.WHITE);

l_tv2.setBackgroundColor(Color.YELLOW);


FrameLayout l_fr = new FrameLayout(this);


RelativeLayout.LayoutParams l_layoutParams = new RelativeLayout.LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


l_layoutParams.leftMargin = 500;

l_layoutParams.topMargin = 100;


l_fr.setLayoutParams(l_layoutParams);


l_fr.addView(l_tv);

l_fr.addView(l_tv2);


((RelativeLayout) findViewById(R.id.layout1)).addView(l_fr);

((RelativeLayout) findViewById(R.id.layout1)).removeView(l_fr);    (1)

((RelativeLayout) findViewById(R.id.layout1)).addView(l_tv);       (2)


Keep an eye on (1). You removed l_fr. So was l_tv removed too? No. If you try to add l_tv like (2). You have a message like below.


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