Android - 来自资产的JSON数组和NullPointerException

时间:2015-06-14 13:12:39

标签: java android json nullpointerexception assets

我想创建一个从本地JSON文件获取数据并将其放入自定义ExpandableListView的应用程序。这是我的代码:

家长班:

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

1 个答案:

答案 0 :(得分:3)

您没有在Parent课程中初始化此ArrayList。

private ArrayList<ChildData> child;

Parent构造函数中,您应该添加:

child = new ArrayList<ChildData>();

我猜这是NullPointerException来自的地方。