2014-01-08 47 views
0

CategoryFragmentAndroid的排球 - 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?我不確定問題所在。是因爲它找不到片段佈局?

如果有任何問題,請讓我知道。

+1

你'arrCategory.'爲空。 –

回答

2

你永遠不會初始化arrCategory。爲適配器創建一個構造函數和分配給arrCategory您在構造函數中提供一個

public class CategoryAdapter extends BaseAdapter { 
    private ArrayList arrCategory; 
    public CategoryAdapter(ArrayList dataSet) { 
     this.arrCategory = dataSet; 
    } 
    // other code 
} 
+0

明白了。忘了補充一個。多謝你們 :) – AimanB

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

@Ketan爲什麼要這樣。? – Raghunandan

+0

這是失敗,所以它的合法性將其發佈爲答案 –

+0

@Raghunandan對不起。我刪除了。 –

1

arrCategory爲空,你應該初始化:

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