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:
My 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"
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"
<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. 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With