2012-07-19 15 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

相關問題