【发布时间】: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