我正在尝试在片段中使用ListView。但我收到此错误:FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class。
我的代码:
我的列表片段:
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