2011-11-27 53 views
2

我有這樣的XML:佈局參考

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/fondoverde3" 
    android:gravity="top" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <com.google.ads.AdView 
      android:id="@+id/ad" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      ads:adSize="BANNER" 
      ads:adUnitId="adasdasdasdas" 
      ads:loadAdOnCreate="true" /> 
    </LinearLayout> 

    <ScrollView 
     android:id="@+id/ScrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <RelativeLayout 
      android:id="@+id/RelativeLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      ...... 

      <LinearLayout 
       android:id="@+id/LinearLayout2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/linearLayout4" 
       android:layout_marginTop="10dp" 
       android:orientation="vertical" > 
      </LinearLayout> 

     </RelativeLayout> 
    </ScrollView> 

</LinearLayout> 

因此,我想在我的Java代碼來引用LinearLayout2的添加一些東西。我用一個簡單的代碼證明要知道,我的代碼工作(和它的作品!我顯示兩個textviews),正是這一點:

LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    TextView tituloIngr2 = new TextView(this); 
tituloIngr2.setText("AAAAA"); 
ll.addView(tituloIngr2); 
    TextView tituloIngr1 = new TextView(this); 
tituloIngr1.setText("BBBBB"); 
ll.addView(tituloIngr1); 

但我想這樣做是在這個linearLayout2許多展示

LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    for (int m= 0; m<few.length; m++) { 

     TextView ingr= new TextView(this); 
     ingr.setText(few[m]); 

     ingr.setPadding(5, 5, 5, 5); 
     ingr.setGravity(0x11);//sacados todos los values de developers 
     ingr.setTextColor(0x96610098); 
     ingr.setBackgroundResource(android.R.color.white); 
     ingr.setTextSize(22); 
     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(           LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    layoutParams.setMargins(0, 25, 0, 0); 
    ll.addView(ingr, layoutParams); 
} 
    this.setContentView(ll); 

的錯誤是一個強制關閉,日誌貓:

11-27 17:09:58.213: E/AndroidRuntime(1440): java.lang.RuntimeException: Unable to start activity ComponentInfo{back.now/back.now.Tree}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
+0

你應該真的使用提供的重力常數而不是魔術十六進制數。 – adamp

回答

2

刪除此呼籲:

,我需要從字符串數組,所以代碼是下一個Textviews
this.setContentView(ll); 

ll已經是內容視圖的一部分。嘗試再次將其添加爲內容視圖可能是觸發異常的原因。您也不需要此呼叫:

ll.setOrientation(LinearLayout.VERTICAL); 

在xml中,方向被指定爲垂直。

+0

只是一個參考:你剛剛標記答案爲「不是一個答案」,而OP仍然在編輯它爲一個更好的答案。我看到了答案的第一次迭代,並且你的國旗有一些優點,所以我將它解釋爲「有用」。但請注意,另一位主持人在稍後審議該版本時完全有理由將其視爲無效。所以請給他們時間。 – NullUserException

+0

@NullUserException - 那是在另一個線程,不是嗎? –

+0

是的。我不想指定哪個答案,但你可以看看你的國旗歷史。 – NullUserException