【问题标题】:your content must have a listview whose id attribute is ' android.r.id.list ' .您的内容必须有一个 id 属性为 'android.r.id.list' 的列表视图。
【发布时间】:2013-04-18 10:27:09
【问题描述】:

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

LogCat 中的错误是:

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

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

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

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>

干杯。

【问题讨论】:

  • 您的代码正在运行.....没问题..
  • 你真的应该从 ListView 中删除这一行 xmlns:android="http://schemas.android.com/apk/res/android"
  • @JonJafari 如果它不起作用,清理,构建然后尝试
  • 感谢您的回答。我再次运行程序,但程序没有运行。我清理并再次构建,但程序没有运行!最后,我在 Eclipse 中创建了一个新项目,然后复制我的代码并粘贴到新项目中,程序运行。出了什么问题?但是当我点击列表中的项目时,程序没有显示选定的项目。为什么?有什么问题?
  • 谢谢。我的问题解决了,程序运行了,但我想知道为什么,当我在 Eclipse 中创建一个新项目时,程序运行正常?

标签: android listview runtime-error


【解决方案1】:

请扩展 Activity 而不是 ListActivity,

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

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

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

而不是调用 setListAdapter()。

【讨论】:

    【解决方案2】:

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

    主活动

    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>
    

    行.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】:

      你必须得到像这样的列表视图的实例

      ListView lv=getListView();
      

      【讨论】:

        【解决方案4】:

        改变这一行

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

         android:id="@+id/list"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多