[英]FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
我正在尝试在片段中使用ListView 。 但我得到这个错误: 致命异常:主java.lang.RuntimeException:内容具有id属性'android.R.id.list'的视图,它不是ListView类 。
我的代码:
我的ListFragment:
public class whitelist_list extends ListFragment {
Context mContext;
@Override
public void onAttach(Activity activity) {
mContext = activity;
Log.i("Event", "onAttach called");
super.onAttach(activity);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
return inflater.inflate(R.layout.whitelist_content, container, false);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something with the data
}
}
whitelist_list newFragment = new whitelist_list();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, newFragment)
.commit();
编辑:解决方案:使用android:id =“@ id / android:list”声明Listview
如果使用ListActivity / Fragment,则布局中ListView的id应为@android:id / list,因此:在ListView中的whitelist_list.xml(whatever_activityname.xml)中将id更改为android:id =“@机器人:ID /列表”
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
我有一段时间没有这个错误。 由于某种原因,布局的XML已被修改,因此其中一个组件的类型错误。 进入xml并查找不属于该项目的'android.R.id.list'。 我不知道它是如何改变的,但我想也许在使用GUI编辑器时,我改变了一些不应该的列表。 如果您没有看到它,请发布布局
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.