您的内容必须具有id属性为';android.r.id.list'。
我编写了程序,但运行此程序时,程序出现运行时错误:( LogCat中的错误是:您的内容必须具有id属性为';android.r.id.list'。,android,listview,runtime-error,Android,Listview,Runtime Error,我编写了程序,但运行此程序时,程序出现运行时错误:( 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 L
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]);
}
}
public类MainActivity扩展了ListActivity{
私有文本查看文本;
私有静态最终字符串[]项={“test”、“test1”、“test2”、“test3”、“test4”、“test5”、“test6”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(新的ArrayAdapter(this,R.layout.row,R.id.label,items));
text=(TextView)findViewById(R.id.selection);
}
public void onListItemClick(ListView父视图、视图v、整数位置、长id){
text.setText(项目[位置]);
}
}
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 lv=getListView();
换行
android:id="@android:id/list"
到
比较你的代码没有什么不同。。。。
主要活动
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);
}
}
}
package com.example.testdemo;
导入android.annotation.SuppressLint;
导入android.app.ActionBar;
导入android.app.Dialog;
导入android.app.ListActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了ListActivity{
私有文本查看文本;
私有静态最终字符串[]项={“test”、“test1”、“test2”、“test3”,
“test4”、“test5”、“test6”};
@SuppressLint(“新API”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u main\u);
setListAdapter(新阵列适配器)(此,R.layout.lable,
R.id.标签、项目);
text=(TextView)findViewById(R.id.selection);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
//TODO自动生成的方法存根
text.setText(“+项[位置]);
super.onListItemClick(左、右、位置、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>
请扩展活动而不是ListActivity 然后从布局文件中获取其视图
ListView listView = (ListView) findViewById(R.id.listView);
然后调用listView.setAdapter(/*agruments*/);
method
不要调用setListAdapter()。您的代码正在工作…..没有问题..您应该从ListView
xmlns:android=”中删除这一行http://schemas.android.com/apk/res/android"
@JonJafari如果它不起作用,请清理、构建并尝试储存答案。我再次运行程序,但程序未运行。我清理并构建程序,但程序未运行!最后我在Eclipse中创建了一个新项目,复制代码并粘贴到新项目中,程序运行。有什么问题吗?但当我单击列表项时,程序没有显示所选项目。为什么?问题是什么?谢谢。我的问题解决了,程序运行了,但我想知道为什么,当我在Eclipse中创建一个新项目时,程序运行正常?
ListView listView = (ListView) findViewById(R.id.listView);