2011-11-27 59 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