Java json listview抛出错误的代码
我想在listview中打印从数据库检索到的json数据,以便应用程序用户能够以可读的格式阅读。但得到以下误差:Java json listview抛出错误的代码,java,android,json,listview,Java,Android,Json,Listview,我想在listview中打印从数据库检索到的json数据,以便应用程序用户能够以可读的格式阅读。但得到以下误差: 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
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();
}
}
}
公共类DisplayListView扩展了AppCompative活动{
字符串json_字符串;
JSONObject JSONObject;
JSONArray JSONArray;
数据适配器数据适配器;
列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.displaylistview\u布局);
listView=(listView)findViewById(R.id.listView);
setAdapter(dataAdapter);
dataAdapter=新的dataAdapter(这个,R.layout.row\u布局);
json_string=getIntent().getExtras().getString(“json_数据”);
Log.w(“var32”,json_字符串);
试一试{
jsonArray=新的jsonArray(json_字符串);
jsonArray=jsonObject.getJSONArray(“_id”);
整数计数=0;
字符串标签位置、RackLocation、ShelfLocation、fourBid、Cluster、fourBookingName、SoftwareVersion、HardwareType、AssetNo、SerialNo;
而(count错误是try块的第一行
您的代码是:
jsonArray = new JSONArray(json_string);
应该是:
jsonObject = new JSONArray(json_string);
这就是出现错误的原因添加以下代码后,错误已修复:
`
试试看{
jsonArray=新的jsonArray(json_字符串);
jsonObject=新的jsonObject();
jsonObject.put(“arrayName”,jsonArray);
jsonArray=jsonObject.getJSONArray(“arrayName”);
整数计数=0;
字符串标签位置、RackLocation、ShelfLocation、fourBid、Cluster、fourBookingName、SoftwareVersion、HardwareType、AssetNo、SerialNO;
而(count通过下面的方法更改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();
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.displaylistview\u布局);
listView=(listView)findViewById(R.id.listView);
setAdapter(dataAdapter);
dataAdapter=新的dataAdapter(这个,R.layout.row\u布局);
json_string=getIntent().getExtras().getString(“json_数据”);
Log.w(“var32”,json_字符串);
试一试{
jsonArray=新的jsonArray(json_字符串);
整数计数=0;
字符串标签位置、RackLocation、ShelfLocation、fourBid、Cluster、fourBookingName、SoftwareVersion、HardwareType、AssetNo、SerialNo;
while(count
您打算更改为jsonObject=new jsonObject(json_string);
?因此,返回的错误是试图对空引用对象调用“org.json.JSONArray org.json.jsonObject.getJSONArray(java.lang.string)”。请仔细检查您的代码并确保所有对象都已初始化。
@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();
}
}