gpt4 book ai didi

java - Android - 来自 Assets 和 NullPointerException 的 JSON 数组

转载 作者:可可西里 更新时间:2023-10-31 22:03:41 28 4
gpt4 key购买 nike

我想制作一个从本地 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 的来源。

关于java - Android - 来自 Assets 和 NullPointerException 的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30829792/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com