Android Volley - Fragment中的setAdapter返回空指针

时间:2014-01-08 10:13:43

标签: android json android-volley

CategoryFragment

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;
    }

}
    }
}

我正在使用Volley来解析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?我不确定问题出在哪里。是因为它无法找到片段布局吗?

如果有的话,请告诉我。

3 个答案:

答案 0 :(得分:2)

private ArrayList<CategoryModel> arrCategory;

永远不会被初始化。

您需要将片段中的arraylist传递给适配器类的构造函数。首先,您需要确保它已填充。

所以使用凌空获取数据。填充列表。

然后

 new  CategoryAdapter(list);
 //this should be after getting data from volley

然后

public class CategoryAdapter extends BaseAdapter {
   private ArrayList<type> arrCategory;
   public CategoryAdapter(ArrayList<type> dataSet) {
      this.arrCategory = dataSet;
   }

答案 1 :(得分:2)

您从未初始化arrCategory。为适配器创建构造函数,并将arrCategory分配给您在构造函数

中提供的构造函数
public class CategoryAdapter extends BaseAdapter {
   private ArrayList arrCategory;
   public CategoryAdapter(ArrayList dataSet) {
      this.arrCategory = dataSet;
   }
  // other code
}

答案 2 :(得分:1)

arrCategory为null,您应该初始化它:

private ArrayList<CategoryModel> arrCategory = new ArrayList<CategoryModel>();