您的内容必须有一个listview,其id属性为'android.r.id.list'。

时间:2013-04-18 10:27:10

标签: android listview runtime-error

我编写程序但是当我运行这个程序时,程序有运行时错误:(

LogCat中的错误是:

your content must have a listview whose id attribute is 'android.r.id.list'

我在android:id="@android:id/list"的activity_main.xml文件中设置了ListView

我写了我的代码,请阅读并帮助我。

MainActivity.java:

public class MainActivity extends ListActivity {

private TextView text;
private static final String[] items={"test", "test1" , "test2" , "test3" , "test4" , "test5" , "test6"};

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this,R.layout.row,R.id.label,items));
    text=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView parent,View v,int position,Long id){
    text.setText(items[position]);
}
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   <TextView
       android:id="@+id/selection"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
   <ListView
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/list"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:drawSelectorOnTop="false"/>
</LinearLayout>

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
   <ImageView
       android:id="@+id/pic"
       android:padding="2dip"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/pic"/>
   <TextView
       android:id="@+id/label"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
</LinearLayout>

干杯。

4 个答案:

答案 0 :(得分:4)

请扩展Activity而不是ListActivity,

然后从布局文件中获取其视图

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

然后调用listView.setAdapter(/*agruments*/);方法

而不是调用setListAdapter()。

答案 1 :(得分:2)

你必须获得像

这样的列表视图的实例
ListView lv=getListView();

答案 2 :(得分:2)

比较你的代码没什么区别....

enter image description here

mainActivity

package com.example.testdemo;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {

    private TextView text;
    private static final String[] items = { "test", "test1", "test2", "test3",
            "test4", "test5", "test6" };

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_);



        setListAdapter(new ArrayAdapter<String>(this, R.layout.lable,
                R.id.label, items));
        text = (TextView) findViewById(R.id.selection);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        text.setText("" + items[position]);
        super.onListItemClick(l, v, position, id);
    }


    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ListView

        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

</LinearLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dip"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

答案 3 :(得分:-3)

更改此行

 android:id="@android:id/list"

 android:id="@+id/list"