方法 oncreateview 中片段中 listview.setadapter() 的空指针异常
Posted
技术标签:
【中文标题】方法 oncreateview 中片段中 listview.setadapter() 的空指针异常【英文标题】:null pointer exception with listview.setadapter() in fragment in method oncreateview 【发布时间】:2016-06-24 10:47:00 【问题描述】:public View onCreateView(LayoutInflater inflater, ViewGroup 容器, 捆绑保存的InstanceState) 查看 v = inflater.inflate(R.layout.fragment_main, container, false);
String[] data =
"Mon 6/23 - Sunny - 31/17",
"Tue 6/24 - Foggy - 21/8",
"Wed 6/25 - Cloudy - 22/17",
"Thurs 6/26 - Rainy - 18/11",
"Fri 6/27 - Foggy - 21/10",
"Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
"Sun 6/29 - Sunny - 20/7"
;
ArrayList<String> listforecast = new ArrayList<String>(Arrays.asList(data));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forcast_,
R.id.listitem_forcast_textview, listforecast);
listView = (ListView) container.findViewById(R.id.listview_display);
listView.setAdapter(adapter);
return v;
片段.xml
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_
android:layout_
android:id="@+id/listview_display"
></ListView>
【问题讨论】:
【参考方案1】:替换:
listView = (ListView) container.findViewById(R.id.listview_display);
与
listView = (ListView) v.findViewById(R.id.listview_display);
【讨论】:
以上是关于方法 oncreateview 中片段中 listview.setadapter() 的空指针异常的主要内容,如果未能解决你的问题,请参考以下文章
方法 oncreateview 中片段中 listview.setadapter() 的空指针异常
Android 片段 onCreateView 与 onActivityCreated