ListView:您的内容必须有一个listview,其id属性为“android.R.id.list”

时间:2015-03-08 11:37:35

标签: android listview

我遇到了ListView.My活动的问题,继承ListViewandroid:id="@android:id/list"的{​​{1}}。我搜索了解决方案,但它没有用。 这是我的配置文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ListView 
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical"></ListView>
</LinearLayout>

我的OnCreate就在这里:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view_demo);
    listView = getListView();
    ArrayList<HashMap<String, String>> list = 
            new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map1 = new HashMap<String,String>();
    HashMap<String, String> map2 = new HashMap<String,String>();
    HashMap<String, String> map3 = new HashMap<String,String>();
    map1.put("user_ip", "user_name");
    map1.put("192.168.25.3", "Maple");
    map2.put("user_ip", "user_name");
    map2.put("192.168.34.3", "Sonny");
    map3.put("user_ip", "user_name");
    map3.put("192.168.25.12", "Eric");
    list.add(map1);
    list.add(map2);
    list.add(map3);

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
            R.layout.user,
            new String[]{"user_ip", "user_name"}, 
            new int[]{R.id.user_ip, R.id.user_name});
    this.setListAdapter(simpleAdapter);
}   

有人帮忙吗?

2 个答案:

答案 0 :(得分:0)

getListView()是ListFragment / ListActivity的具体方法你使用其中一个吗? 删除:

  

listView = getListView();

答案 1 :(得分:-1)

初始化listView的方式是错误的。

更改,

private ListView listView = getListView();

ListView listView = (ListView) findViewById(R.id.list);

如果您设置contentView ,则无需将活动扩展到ListActivity