ListView问题:Your content must have a ListView wh...

最近学习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!

转载于:https://my.oschina.net/weiyanen/blog/91861

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值