Android使用ListView控件问题:
The application has stopped unexpectedly, please try again。
开发环境:android 1.6 最低兼容 4.0
报错代码如下:
main.xml
2 <LinearLayout
3 android:id="@+id/LinearLayout01"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 xmlns:android="http://schemas.android.com/apk/res/android">
7
8 <ListView android:layout_width="wrap_content"
9 android:layout_height="wrap_content"
10 android:id="@+id/
2 /** Called when the activity is first created. */
3 private ListView listv;
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.main);
8 listv = (ListView) findViewById(R.id.mylistview);
9
10 //生成动态数组,并且转载数据
11 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
12 for(int i=0;i<30;i++)
13 {
14 HashMap<String, String> map = new HashMap<String, String>();
15 map.put("ItemTitle", "This is Title"+i);
16 map.put("ItemText", "This is text "+i);
17 mylist.add(map);
18 }
19 //生成适配器,数组===》ListItem
20 SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释
21 mylist,//数据来源
22 R.layout.my_listitem,//ListItem的XML实现
23
24 //动态数组与ListItem对应的子项
25 new String[] { "ItemTitle", "ItemText"},
26
27 //ListItem的XML文件里面的两个TextView ID
28 new int[] {R.id.ItemTitle,R.id.ItemText});
29 //添加并且显示
30 listv.setAdapter(mSchedule);
31
32
33
34 }
35 }
">
11 ListView>
12 Linear
listview的布局文件 my_listview.xml
2 <LinearLayout
3 android:layout_width="fill_parent"
4 xmlns:android="http://schemas.android.com/apk/res/android"
5 android:orientation="vertical"
6 android:layout_height="wrap_content"
7 android:id="@+id/MyListItem"
8 android:paddingBottom="3dip"
9 android:paddingLeft="10dip">
10 <TextView
11 android:layout_height="wrap_content"
12 android:layout_width="fill_parent"
13 android:id="@+id/ItemTitle"
14 android:textSize="30dip">
15 TextView>
16 <TextView
17 android:layout_height="wrap_content"
18 android:layout_width="fill_parent"
19 android:id="@+id/ItemText">
20 TextView>
21 LinearLayout>
22
java代码
修改后的main.xml:
修改后的java
2 /** Called when the activity is first created. */
3 private ListView listv;
4 @Override
5 public void onCreate(Bundle savedInstanceState) {
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.main);
8 listv = (ListView) findViewById(android.R.id.list);
9 //生成动态数组,并且转载数据
10 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
11 for(int i=0;i<30;i++)
12 {
13 HashMap<String, String> map = new HashMap<String, String>();
14 map.put("ItemTitle", "This is Title"+i);
15 map.put("ItemText", "This is text "+i);
16 mylist.add(map);
17 }
18 //生成适配器,数组===》ListItem
19 SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释
20 mylist,//数据来源
21 R.layout.my_listitem,//ListItem的XML实现
22
23 //动态数组与ListItem对应的子项
24 new String[] { "ItemTitle", "ItemText"},
25
26 //ListItem的XML文件里面的两个TextView ID
27 new int[] {R.id.ItemTitle,R.id.ItemText});
28 //添加并且显示
29 listv.setAdapter(mSchedule);
30
31
32
33 }
34 }