我在ListView上设置ArrayAdapter时面临NullPointerException。
下面提到了代码和例外情况。由于数据是在列表中提供的..我知道它不是NULL。 那么为什么这个问题....任何帮助或指针真的很感激!
这是代码: MainActivityFragment.java
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
List list = new ArrayList<String>();
list.add("Today - Sunny - 88/63");
list.add("Tomorrow - Foggy - 88/63");
list.add("Wednesday - Cloudy - 88/63");
list.add("Thursday - Rainy - 88/63");
list.add("Friday - Sunny - 88/63");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.fragment_main,
R.id.list_item_forecast_textview,
list);
ListView listView = (ListView) view.findViewById(R.id.list_view_forecast);
listView.setAdapter(arrayAdapter);
return view;
}
}
例外:
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillDown(ListView.java:691)
at android.widget.ListView.fillFromTop(ListView.java:752)
at android.widget.ListView.layoutChildren(ListView.java:1630)
答案 0 :(得分:4)
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.fragment_main,
R.id.list_item_forecast_textview,
list);
在R.layout.fragment_main
您应该通过ListItem Layout
此处
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.yourListItemLayout,
R.id.list_item_forecast_textview,
list);