0

Em 얻기 java.lang.IllegalStateException 발생 : 지정된 자식에 이미 상위가 있습니다. 먼저 부모의 부모에 대해 removeView()를 호출해야합니다.Java 비정상 상태 가져 오기 XML을 확장하여보기를 추가 할 때 예외가 발생했습니다.

어떤보기를 제거해야하는지 알 수 없으면 도움이 될 것입니다. 여기

는 코드 스 니펫입니다

main.xml에

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:id="@+id/mainLayout" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

</RelativeLayout> 

인 data.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

</RelativeLayout> 

활동 코드

public class ToDo extends Activity { 
    /** Called when the activity is first created. */ 
    Button addNew; 
    RelativeLayout mainLayout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mainLayout=(RelativeLayout)findViewById(R.id.mainLayout); 

     RelativeLayout rel; 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     for(int idx=0;idx<2;idx++){ 
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

      rel = (RelativeLayout) inflater.inflate(R.layout.data,null); 
      params.setMargins(0, 50, 0, 0); 

      TextView fromWeb= (TextView) rel.findViewById(R.id.txt); 
      fromWeb.setText("AA"); 

      mainLayout.addView(rel,params); 
     } 

    } 
} 
+0

확인이 링크를 : http://stackoverflow.com/questions/10007094/java-lang-illegalstateexception-the-specified-child-already-has-a-parent –

+0

시도하고 확인하자! – Badrinath

답변

0

아래의 코드가 올바르지 않습니다

TextView fromWeb= (TextView) rel.findViewById(R.id.txt); 
fromWeb.setText("AA"); 
rel.addView(fromWeb,params); // the TextView is alredy in the rel RelativeLayout! 
mainLayout.addView(rel); 

당신이 다시 (이전 findViewById으로 검색으로) 레이아웃 파일에 이미있는 TextView합니다 (addView 방법)으로 추가되기 때문에. 대신 이렇게해야합니다 :

TextView fromWeb= (TextView) rel.findViewById(R.id.txt); 
fromWeb.setText("AA"); 
mainLayout.addView(rel); 

당신이 TextView 다음 레이아웃 파일에 설정하여 여백하려면 다음

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:text="TextView" /> 

</RelativeLayout> 
+0

이 맞습니다! 귀하의 편집 된 코드를 시도했을 때 작동하지만, 텍스트를 overlapping.even 비록 내가 상대 레이아웃에 대한 매개 변수를 사용합니다. 편집 된 코드 – Badrinath

+0

@Badrinath를 확인하십시오.'orientation'이'vertical'으로 설정된'LinearLayout' 대신'main.xml' 파일에서'RelativeLayout'을 사용하기 때문입니다. – Luksprog

+0

여러 개의 텍스트 뷰를 하나씩 추가해야합니다. 그래서 풍선 메카니즘을 사용했습니다. – Badrinath