I'm trying to use a ListView in a fragment. 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.
My Code:
我在片段中使用ListView。但是我得到了这个错误:致命异常:main java.lang。RuntimeException:内容具有id属性' android.r id的视图。列出“那不是ListView类。我的代码:
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
2 个解决方案
#1
17
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,因此:在您的whitelist_list中。ListView中的xml (whatever_activityname.xml)将id更改为android:id=“@android:id/list”
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
#2
1
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. Go into the xml and look for the 'android.R.id.list' that doesn't belong to that item. 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. If you don't see it, post the layout
我以前犯过这个错误。由于某些原因,布局的XML已经被修改,因此其中一个组件的类型是错误的。进入xml并查找“android.R.id”。列出不属于那个项目的。我不知道它是如何被修改的,但是我想也许在使用GUI编辑器时,我修改了一些不应该被修改的内容。如果你没有看到,发布布局
#1
17
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,因此:在您的whitelist_list中。ListView中的xml (whatever_activityname.xml)将id更改为android:id=“@android:id/list”
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
#2
1
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. Go into the xml and look for the 'android.R.id.list' that doesn't belong to that item. 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. If you don't see it, post the layout
我以前犯过这个错误。由于某些原因,布局的XML已经被修改,因此其中一个组件的类型是错误的。进入xml并查找“android.R.id”。列出不属于那个项目的。我不知道它是如何被修改的,但是我想也许在使用GUI编辑器时,我修改了一些不应该被修改的内容。如果你没有看到,发布布局