2010-06-29 58 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]); 
相关问题