2015-06-14 19 views
3

我想創建一個從本地JSON文件獲取數據並將其放入自定義ExpandableListView的應用程序。這裏是我的代碼:Android - 來自資產和NullPointerException的JSON數組

父類:

public class Parent { 

    private String name; 
    private ArrayList<ChildData> child; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public ArrayList<ChildData> getChild() { 
     return child; 
    } 

    public void setChild(ArrayList<ChildData> child) { 
     this.child = child; 
    } 
} 

ChildData類:

public class ChildData { 
    private String opis1; 
    private String opis2; 
    private String img_id; 

    public String getOpis1() { 
     return opis1; 
    } 

    public void setOpis1(String opis1) { 
     this.opis1 = opis1; 
    } 

    public String getOpis2() { 
     return opis2; 
    } 

    public void setOpis2(String opis2) { 
     this.opis2 = opis2; 
    } 

    public String getImg_id() { 
     return img_id; 
    } 

    public void setImg_id(String img_id) { 
     this.img_id = img_id; 
    } 
} 

MainActivity類別:

public class MainActivity extends ExpandableListActivity { 

    private ArrayList<Parent> parents; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     getExpandableListView().setGroupIndicator(null); 
     getExpandableListView().setDividerHeight(1); 
     registerForContextMenu(getExpandableListView()); 

     //Creating static data in arraylist 
     final ArrayList<Parent> dataList = putDataIntoArrays(); 

     // Adding ArrayList data to ExpandableListView values 
     loadHosts(dataList); 

    } 


    public String loadJSONFromAsset() { 
     String json = null; 
     try { 

      InputStream is = getAssets().open("data.json"); 

      int size = is.available(); 

      byte[] buffer = new byte[size]; 

      is.read(buffer); 

      is.close(); 

      json = new String(buffer, "UTF-8"); 


     } catch (IOException ex) { 
      ex.printStackTrace(); 
      return null; 
     } 
     return json; 

    } 

    public ArrayList<Parent> putDataIntoArrays() { 
     JSONObject obj = null; 

     ArrayList<Parent> list = new ArrayList<>(); 
     Parent parent = new Parent(); 
     ChildData cd = new ChildData(); 

     try { 
      obj = new JSONObject(loadJSONFromAsset()); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     try { 
      JSONArray m_jArry = obj.getJSONArray("data"); 

      for (int i = 0; i < m_jArry.length(); i++) { 

       JSONObject jo_inside = m_jArry.getJSONObject(i); 

       parent.setName(jo_inside.getString("title")); 
       Log.d("Test", "Parent name: " + parent.getName()); 

       cd.setOpis1(jo_inside.getString("desc")); 
       cd.setOpis2(jo_inside.getString("desc2")); 
       cd.setImg_id(jo_inside.getString("img")); 
       Log.d("Test", "O1: " + cd.getOpis1()); 
       Log.d("Test", "O2: " + cd.getOpis2()); 
       Log.d("Test", "IMG: " + cd.getImg_id()); 

       parent.getChild().add(cd); 

       Log.d("Test", "Parent: " + parent.getChild()); 

       list.add(parent); 
       Log.d("Test", "List: " + list.get(i)); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return list; 
    } 

    private void loadHosts(final ArrayList<Parent> newParents) 
    { 
     if (newParents == null) 
      return; 

     parents = newParents; 

     // Check for ExpandableListAdapter object 
     if (this.getExpandableListAdapter() == null) 
     { 
      //Create ExpandableListAdapter Object 
      final ExpandableListAdapter mAdapter = new ExpandableListAdapter(); 

      // Set Adapter to ExpandableList Adapter 
      this.setListAdapter(mAdapter); 
     } 
     else 
     { 
      // Refresh ExpandableListView data 
      ((ExpandableListAdapter)getExpandableListAdapter()).notifyDataSetChanged(); 
     } 
    } 
} 

和data.json

{ 
"data": [ 
{ 
    "title": "T1", 
    "desc": "D1", 
    "desc2": "opis1", 
    "img": "1" 
}, 
{ 
    "title": "T2", 
    "desc": "D2", 
    "desc2": "opis2", 
    "img": "2" 
}, 
{ 
    "title": "T3", 
    "desc": "D3", 
    "desc2": "opis3", 
    "img": "3" 
}, 
{ 
    "title": "T4", 
    "desc": "D4", 
    "desc2": "opis4", 
    "img": "1" 
} 
    ] 
} 

而且當我運行我的應用程序,我沒有得到任何結果從日誌:

Log.d("Test", "Rodzic1: " + parent.getChild()); 
Log.d("Test", "List: " + list.get(i)); 
+2

異常發生在最後一行ArrayList dataList = putDataIntoArrays();由於方法putDataIntoArrays();但Gosu的回答解決了這個問題:) – gkopowski

回答

3

你沒有在你的Parent類初始化此ArrayList。

private ArrayList<ChildData> child; 

在你Parent構造函數,你應該增加:

child = new ArrayList<ChildData>(); 

我猜測是你NullPointerException的來源。

+0

謝謝!這是問題所在。順便說一句。這種方法是否足夠顯示用戶可展開的列表,並且擁有超過100個父對象?還是應該使用別的東西? – gkopowski