[英]FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
I'm trying to use a ListView in a fragment . 我正在尝试在片段中使用ListView 。 But i get this Error: FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class . 但我得到这个错误: 致命异常:主java.lang.RuntimeException:内容具有id属性'android.R.id.list'的视图,它不是ListView类 。
My Code: 我的代码:
My ListFragment: 我的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();
Edit: Solution: Declare Listview with android:id="@id/android:list" 编辑:解决方案:使用android:id =“@ id / android:list”声明Listview
If you use a ListActivity/Fragment, the id for your ListView in your layout should be @android:id/list, so: In your whitelist_list.xml (whatever_activityname.xml) in your ListView change the id to android:id="@android:id/list" 如果使用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"/>
I had this error a while back. 我有一段时间没有这个错误。 For some reason the XML for the layout had been modified so that one of the components had the wrong type. 由于某种原因,布局的XML已被修改,因此其中一个组件的类型错误。 Go into the xml and look for the 'android.R.id.list' that doesn't belong to that item. 进入xml并查找不属于该项目的'android.R.id.list'。 I don't know how it got changed, but I think perhaps that when using the GUI editor, I changed something to list that shouldn't have been. 我不知道它是如何改变的,但我想也许在使用GUI编辑器时,我改变了一些不应该的列表。 If you don't see it, post the layout 如果您没有看到它,请发布布局
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.