资产和NullPointerException中的JSON数组

资产和NullPointerException中的JSON数组,第1张

概述我想制作一个从本地JSON文件中获取数据并将其放入自定义ExpandableListView中的应用.这是我的代码:家长班:publicclassParent{privateStringname;privateArrayList<ChildData>child;publicStringgetName(){returnname;}publicvo

我想制作一个从本地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));

解决方法:

您没有在Parent类中初始化此ArrayList.

private ArrayList<ChildData> child;

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

child = new ArrayList<ChildData>();

我猜这就是您的NullPointerException的来源.

总结

以上是内存溢出为你收集整理的资产和NullPointerException中的JSON数组全部内容,希望文章能够帮你解决资产和NullPointerException中的JSON数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1080029.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存