我想在listview中打印从数据库中检索到的json数据,以便它可以是app用户的可读格式。但是获得了以下错误:
07-20 18:22:09.541 29095-29095/com.example.manv.nokialabs I/Timeline: Timeline: Activity_launch_request id:com.example.manv.nokialabs time:36794948
--------- beginning of system
07-20 18:22:09.591 29095-29095/com.example.manv.nokialabs W/var32: [{ "_id" : { "$oid" : "5784818bcb30b4918964b50f"} , "LabLocation" : "LSD-SRN-BLR-U02" , "RackLocation" : "SRN-Radio-B01" , "ShelfLocation" : "SH-01" , "fourBid" : "908762" , "Cluster" : "Radio" , "fourBookingName" : "BTS01-Coral-SRN-BLR" , "SoftwareVersion" : "LN6.0" , "HardwareType" : "FRGP" , "AssetNo" : "40824873" , "SerialNO" : "RY114208612" , "Location" : "zzccg" , "Uname" : "addtvI"}]
07-20 18:22:09.591 29095-29095/com.example.manv.nokialabs D/AndroidRuntime: Shutting down VM
--------- beginning of crash
07-20 18:22:09.592 29095-29095/com.example.manv.nokialabs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.manv.nokialabs, PID: 29095
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.manv.nokialabs/com.example.albbaby.nokialabs.DisplayListView}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at com.example.albbaby.nokialabs.DisplayListView.onCreate(DisplayListView.java:32)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) )
这是我的代码:
public class DisplayListView extends AppCompatActivity {
String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
dataAdapter dataAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaylistview_layout);
listView=(ListView) findViewById(R.id.listview);
listView.setAdapter(dataAdapter);
dataAdapter=new dataAdapter(this,R.layout.row_layout);
json_string=getIntent().getExtras().getString("json_data");
Log.w("var32", json_string);
try {
jsonArray = new JSONArray(json_string);
jsonArray=jsonObject.getJSONArray("_id");
int count=0;
String LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNo;
while (count<jsonObject.length()){
JSONObject JO=jsonArray.getJSONObject(count);
LabLocation=JO.getString("LabLocation");
RackLocation=JO.getString("RackLocation");
ShelfLocation=JO.getString("ShelfLocation");
fourBid=JO.getString("fourBid");
Cluster=JO.getString("Cluster");
fourBookingName=JO.getString("fourBookingName");
SoftwareVersion=JO.getString("SoftwareVersion");
HardwareType=JO.getString("HardwareType");
AssetNo=JO.getString("AssetNo");
SerialNo=JO.getString("SerialNo");
data data=new data(LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNo);
dataAdapter.add(data);
count++;
}
}catch (JSONException e){
Log.w("var33", "error JSON exception");
e.printStackTrace();
}
}
}
请帮忙调试。 感谢
答案 0 :(得分:0)
错误是try block的第一行 你编码:
jsonArray = new JSONArray(json_string);
应该是:
jsonObject = new JSONArray(json_string);
这就是错误即将到来的原因
答案 1 :(得分:0)
添加以下代码后错误已修复: `
try {
jsonArray = new JSONArray(json_string);
jsonObject = new JSONObject();
jsonObject.put("arrayName",jsonArray);
jsonArray=jsonObject.getJSONArray("arrayName");
int count=0;
String LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNO;
while (count<jsonObject.length()){
JSONObject JO=jsonArray.getJSONObject(count);
LabLocation=JO.getString("LabLocation");
RackLocation=JO.getString("RackLocation");
ShelfLocation=JO.getString("ShelfLocation");
fourBid=JO.getString("fourBid");
Cluster=JO.getString("Cluster");
fourBookingName=JO.getString("fourBookingName");
SoftwareVersion=JO.getString("SoftwareVersion");
HardwareType=JO.getString("HardwareType");
AssetNo=JO.getString("AssetNo");
SerialNO=JO.getString("SerialNO");
Log.w("var34", "Arguments Passed");
data data=new data(LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNO);
dataAdapter.add(data);
count++;
}
`
答案 2 :(得分:0)
通过以下方式更改您的onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaylistview_layout);
listView=(ListView) findViewById(R.id.listview);
listView.setAdapter(dataAdapter);
dataAdapter=new dataAdapter(this,R.layout.row_layout);
json_string=getIntent().getExtras().getString("json_data");
Log.w("var32", json_string);
try {
jsonArray = new JSONArray(json_string);
int count=0;
String LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNo;
while (count < jsonArray.length())
{
JSONObject JO=jsonArray.getJSONObject(count);
jsonObject = JO.getJSONObject("_id");
LabLocation=JO.getString("LabLocation");
RackLocation=JO.getString("RackLocation");
ShelfLocation=JO.getString("ShelfLocation");
fourBid=JO.getString("fourBid");
Cluster=JO.getString("Cluster");
fourBookingName=JO.getString("fourBookingName");
SoftwareVersion=JO.getString("SoftwareVersion");
HardwareType=JO.getString("HardwareType");
AssetNo=JO.getString("AssetNo");
SerialNo=JO.getString("SerialNo");
data data=new data(LabLocation,RackLocation,ShelfLocation,fourBid,Cluster,fourBookingName,SoftwareVersion,HardwareType,AssetNo,SerialNo);
dataAdapter.add(data);
count++;
}
}catch (JSONException e){
Log.w("var33", "error JSON exception");
e.printStackTrace();
}
}