最近学习android开发,在android developers中练习Tutrials中的Hello view子教程 listview,使用ListActivity来生成新的activity发生问题:
public class ListViewActivity extends ListActivity
内部使用函数设置adapter
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
在调试器中运行出现问题,logcat中的内容如下:
04-14 02:15:11.326: E/AndroidRuntime(843): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.listview/com.sample.listview.ListViewActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
搜索java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list',发现类似的问题很多,解决方法,在main.xml中添加一个ListView的内容。经过测试,该方法有效。
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
个人认为,引起该问题的原因是由于ListActivity内部含有ListView,而该ListView没有xml来描述界面。
不过对比Autocomplete教程,若让ListViewActivity继承Activity,自己在界面上添加一个ListView,然后通过id找到它同样可以解决该问题:
public class ListViewActivity extends Activity
修改code:
ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
lv.setTextFilterEnabled(true);
修改xml:
<ListView android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
经过测试,完全ok!
public class ListViewActivity extends ListActivity
内部使用函数设置adapter
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
在调试器中运行出现问题,logcat中的内容如下:
04-14 02:15:11.326: E/AndroidRuntime(843): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.listview/com.sample.listview.ListViewActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
搜索java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list',发现类似的问题很多,解决方法,在main.xml中添加一个ListView的内容。经过测试,该方法有效。
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
个人认为,引起该问题的原因是由于ListActivity内部含有ListView,而该ListView没有xml来描述界面。
不过对比Autocomplete教程,若让ListViewActivity继承Activity,自己在界面上添加一个ListView,然后通过id找到它同样可以解决该问题:
public class ListViewActivity extends Activity
修改code:
ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
lv.setTextFilterEnabled(true);
修改xml:
<ListView android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
经过测试,完全ok!