2012-07-19 46 views
0

中的onCreate图解创建一个ListView

TextView tv = new TextView(this); 
tv.setId(1); 
ListView lView = new ListView(this); 
String[] lStr = new String[] { "A", "B", "C" }; 
ArrayAdapter<String> lAdap = new ArrayAdapter<String>(this, tv.getId(), lStr); 
lView.setAdapter(lAdap); 
setContentView(lView); 

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dtl/com.example.dtl.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x2 

回答

0

尝试使用android.R.layout.simple_list_item_1代替textview

0

使用此:

YourList.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, YourArray)); 
+0

是工作太谢谢 – vhong 2012-07-23 02:19:33