2013-07-10 22 views
0

首先添加幾個TextViews,這裏是我的代碼:IllegalStateException異常:無法在查看

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     LinearLayout linLayout = new LinearLayout(this); 

     TextView text=new TextView(this); 
     ArrayList<String> recup = recupData(); 
     List<TextView> lvariables = new ArrayList<TextView>(); 
     int i = 0; 

     for(i=0;i<recup.size();i++){ 
      text.setText(recup.get(i)+"\n"); 
      lvariables.add(text); 
      linLayout.addView(lvariables.get(i)); 
     } 
     setContentView(linLayout); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

現在,我的問題是:如何顯示的N個textviews(在迴路中產生並存儲在我的列表)在相同的佈局(一個在另一個例如)!?

隨着代碼你可以看到,我收到一個「IllegalState」異常!

這裏的XML文件,以防萬一:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 
</RelativeLayout> 

(短是不是!?/)

我conscient這個問題是基本的和愚蠢的,但我可以」讓我的大腦理解Android哲學的工作方式!

如果有人有足夠的耐心來幫助我解決我的問題,即使它已經在StackOverFlow上討論過一百萬次,但我會非常感激!

我真的很感謝您可以提供任何幫助! 哦,請原諒我的英語,我是法國人...:d

+0

你需要n個textviews。你可以隨時追加數據到現有的textview – Raghunandan

+0

我想把所有的東西放在同一個textview中,但我不能,因爲我想獨立地格式化字符串樣式! – BigIndian66

+0

你可以使用sppananble字符串來獨立設計你的單詞 – Raghunandan

回答

2

要添加總是相同的TextView至極不允許

變化:

for(i=0;i<recup.size();i++){ 
     text.setText(recup.get(i)+"\n"); 
     lvariables.add(text); 
     linLayout.addView(lvariables.get(i)); 
} 

for(i=0;i<recup.size();i++){ 
     TextView text=new TextView(this); 
     text.setText(recup.get(i)+"\n"); 
     lvariables.add(text); 
     linLayout.addView(text); 
} 

編輯:要改變你的LinearLayout的方向垂直:

LinearLayout linLayout = new LinearLayout(this); 
linLayout.setOrientation(LinearLayout.VERTICAL); 
+0

我糾正了它,但只有三個字符串中的一個出現了! – BigIndian66

+0

LinearLayout的默認方向是水平的。你必須改變它垂直 – Blackbelt

+0

它的工作,謝謝你! – BigIndian66

相關問題