CategoryFragment:Android的排球 - setAdapter在片段返回空指針
public class CategoryFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_category, container,
false);
va = new CategoryAdapter();
listView = (ListView) rootView.findViewById(R.id.catListView);
listView.setAdapter(va);
...
JsonArrayRequest jr = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
...
return rootView;
}
CategoryAdapter:
public class CategoryAdapter extends BaseAdapter {
private ArrayList<CategoryModel> arrCategory;
private LayoutInflater lf;
@Override
public int getCount() {
return arrCategory.size();
}
@Override
public Object getItem(int i) {
return arrCategory.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder vh;
if (view == null) {
vh = new ViewHolder();
view = lf.inflate(R.layout.row_category_listview, null);
vh.tvTitle = (TextView) view.findViewById(R.id.txtTitle);
vh.tvCount = (TextView) view.findViewById(R.id.txtCount);
view.setTag(vh);
} else {
vh = (ViewHolder) view.getTag();
}
CategoryModel nm = arrCategory.get(i);
vh.tvTitle.setText(nm.getTitle());
vh.tvCount.setText(nm.getCount());
return view;
}
private class ViewHolder {
TextView tvTitle;
TextView tvCount;
}
}
}
}
我使用抽射從API解析JSON。我的項目的主要活動是處理導航抽屜,所以我必須使用CategoryFragment
來顯示類別的列表視圖。
然而,通過運行上面的代碼,我得到這個錯誤:
01-08 17:43:37.950: E/AndroidRuntime(9897): java.lang.NullPointerException
01-08 17:43:37.950: E/AndroidRuntime(9897): at com.package.adapter.VolleyAdapter.getCount(VolleyAdapter.java:21)
01-08 17:43:37.950: E/AndroidRuntime(9897): at android.widget.ListView.setAdapter(ListView.java:462)
01-08 17:43:37.950: E/AndroidRuntime(9897): at com.package.fragment.CategoryFragment.onCreateView(CategoryFragment.java:54)
任何人都知道爲什麼它設置適配器時返回nullpointerexception
?我不確定問題所在。是因爲它找不到片段佈局?
如果有任何問題,請讓我知道。
你'arrCategory.'爲空。 –