Android 在listfragment中使用getview时应用程序崩溃
我正在学习如何使用ListFragment,而应用程序在运行以下代码时会崩溃:Android 在listfragment中使用getview时应用程序崩溃,android,Android,我正在学习如何使用ListFragment,而应用程序在运行以下代码时会崩溃: public class profileListFragment extends ListFragment { String[] countries = new String[] {"USA", "China"}; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc
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);
}
}
我制作了一个如下所示的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" >
</ListView>
</LinearLayout>
发布坠机的日志。嗯。。。我下载了日志,但不知道如何获取日志。。。另外,我应该提到的是,我正在平板电脑上运行它,而不是在模拟器上运行,不确定这是否重要……你真的读过catlog的说明了吗?或者试试菜单选项?您可以通过电子邮件将整个logcat输出发送给您自己,然后将其复制/粘贴到此处(尽管请仅复制/粘贴与崩溃相关的部分)。或者,使用USB连接平板电脑并使用DDMS。可能是因为在onCreate()中执行getListView时列表没有膨胀。检查充气机是否为空,以及其是否为充气
查看视图=充气机。充气(R.layout.yourlayout,container,false)代码>嗯。。。不知道DDMS和catlog会抱怨sudo权限。。。。无论如何,我认为使用DDMS更新了相关信息,如果需要其他信息,请告诉我。谢谢
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" >
</ListView>
</LinearLayout>
View retView = inflater.inflate(R.layout.fragment_load_profile_list_layout,container, false);
ListView lv = (ListView) retView.findViewById(android.R.id.list);
lv.setSelector(android.R.color.holo_blue_light);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);