我正在学习如何使用 ListFragment 并且应用程序在运行以下代码时崩溃:
public class profileListFragment extends ListFragment {
String[] countries = new String[] {"USA", "China"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(),
android.R.layout.simple_list_item_1,countries);
setListAdapter(adapter);
View retView = super.onCreateView(inflater, container, savedInstanceState);
return retView;//super.onCreateView(inflater, container, savedInstanceState);
}
public void onListItemClick(ListView I, View v, int position, long id){
I.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
I.setSelector(android.R.color.holo_blue_light);
}
}
我知道这是因为 getListView() 语句,因为当我删除它时它不会崩溃......
非常感谢任何帮助
I/ActivityManager(352): START {cmp=myapp.app/.loadProfile u=0} from pid 24295
E/AndroidRuntime(24295): java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp.app/myapp.app.loadProfile}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
E/AndroidRuntime(24295): at myapp.app.loadProfile.onCreate(loadProfile.java:12)
E/AndroidRuntime(24295): at myapp.app.profileListFragment.onCreateView(profileListFragment.java:19)
11-10 22:15:01.002: W/ActivityManager(352): Force finishing activity myapp.app/.loadProfile