2010-06-29 39 views
0

這應該很簡單,但是會讓我瘋狂。從數組中保存的字符串資源設置textview文本

我在佈局中有以下,而不是問題。

<TextView 
    android:id="@+id/birdinfo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#00009c" 
    android:text="The Robin is a popular bird" 
/> 

然後我有設置了字符串資源的列表,這些陣列我有

private Integer[] DetailIds = {    
     R.string.barnaclegoose, 
     R.string.barnowl, 
     R.string.bewicksswan, 
     R.string.blackbird, 
     R.string.blackcap_male}; 

所以我只是想做到這一點!

TextView detail = (TextView)findViewById(R.id.birdinfo); 
    detail.setText(DetailIds[0]); 
    setContentView(R.layout.main); 

但是這會導致強制關閉錯誤。

字符串資源看起來是這樣的(當然沒有頁眉和頁腳信息

<string name="barnaclegoose">What a wonderful goose!</string> 

添加到這個問題,如果我直接使用資源到資源

detail.setText(R.string.barnaclegoose); 

例如,我仍然得到一個零例外!我確信我以前做過這個,但也許我錯過了明顯???

任何想法讚賞。

(Eclipse,Android 1.5,Emulator with 1.5)

+0

你確定,你可以在整型內舉行的字符串陣列? ;-) – poeschlorn 2010-06-29 11:15:55

+0

發佈表示的堆棧跟蹤 – Budius 2016-06-25 09:10:00

回答

0

感謝您的回答。但是如果您的意思是R.string.barnaclegoose,則這是指向資源中字符串本身的ID的整數值。

無論如何,我終於通過創建內聯視圖而不是使用資源視圖來工作。

例如

TextView t= new TextView(ctx); 
    t.setId(2); 
    t.setTextColor(Color.BLACK); 
    t.setText(DetailIds[bird]); 

    mLinearLayout.addView(t,params); 
    mLinearLayout.setBackgroundColor(Color.WHITE); 
    setContentView(mLinearLayout); 

這完美的作品。

0

我意識到這是很老,但它在搜索想出了......反正,你需要調用setContentView()findViewById()前等:

setContentView(R.layout.main); 
TextView detail = (TextView)findViewById(R.id.birdinfo); 
detail.setText(DetailIds[0]); 
相關問題