我有一个跟随jsonarray,我想解析它,但我得到空指针异常。网址在浏览器中工作,数据如下
{"showDriver":
[{"IDDriver":"1","Driverlogtit":"24.614095","Driverlatite":"46.509689"},
{"IDDriver":"2","Driverlogtit":"24.639199","Driverlatite":"46.557047"},
{"IDDriver":"3","Driverlogtit":"24.651724","Driverlatite":"46.559131"},
{"IDDriver":"4","Driverlogtit":"24.625951","Driverlatite":"46.569466"},
{"IDDriver":"8","Driverlogtit":"24.626692","Driverlatite":"46.544095"}]}
在android中调用json时得到我的错误
java.lang.NullPointerException
at com.project.ytubasi.speedtaxi.MapsActivity.WhereDriver(MapsActivity.java:329)
at com.project.ytubasi.speedtaxi.MapsActivity$3.onCameraIdle(MapsActivity.java:166)
at com.google.android.gms.maps.GoogleMap$22.onCameraIdle(Unknown Source)
at com.google.android.gms.maps.internal.zzf$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:361)
at ys.a(:com.google.android.gms.DynamiteModulesB:79)
at maps.ad.j.b(Unknown Source)
at maps.ad.j.a(Unknown Source)
at maps.ad.j$2.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:855)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
at dalvik.system.NativeStart.main(Native Method)
我班上有任何不妥之处
这里打电话
mGoogleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
CameraLat = googleMap.getCameraPosition().target.latitude;
CameraLong = googleMap.getCameraPosition().target.longitude;
markerlocationtext.setText(getCompleteAddressString(CameraLat, CameraLong));
WhereDriver(String.valueOf(CameraLong), String.valueOf(CameraLat));
String lat = DriverList.get(0).Driverlatite;
Toast.makeText(getApplicationContext(),""+lat,Toast.LENGTH_LONG).show();
}
});
}
我的数据类
AQuery aq;
public void WhereDriver(String longt ,String lati) {
String url=muUrl+"WhereDriver?logt=24.632652&lat=46.558625";//+longt+"&lat="+lati; i used stable Number to see data
aq.ajax(url, JSONObject.class, this, "jsonCallback");
}
public void jsonCallback(String url, JSONObject json, AjaxStatus status) throws JSONException {
if(json != null){
try {
JSONArray DriverArray = json.getJSONArray("showDriver");
for (int z = 0; z < DriverArray.length(); z++) {
JSONObject jsDriverArray = DriverArray.getJSONObject(z);
DriverList.add(new AllDriverLocation(jsDriverArray.getString("IDDriver"),
jsDriverArray.getString("Driverlogtit"),
jsDriverArray.getString("Driverlatite")));
}
}catch (Exception ex)
{
}
}else{
}
}
我将我的列表定义为
public ArrayList <AllDriverLocation> DriverList = new ArrayList<>();
和AllDriverLocation类
public class AllDriverLocation {
String IDDriver;
String Driverlogtit;
String Driverlatite;
public AllDriverLocation(String IDDriver, String driverlogtit, String driverlatite) {
this.IDDriver = IDDriver;
Driverlogtit = driverlogtit;
Driverlatite = driverlatite;
}
}
答案 0 :(得分:0)
这个问题似乎就在这一行 -
SELECT T1.S.no FROM Your_tableName T1 WHERE T1.SampleNo = (
SELECT MAX(T2.SampleNo) FROM Your_tableName T2 WHERE T2.SampleNo BETWEEN @val1 AND @val2 );
请检查变量CameraLong和CameraLat是否为空
答案 1 :(得分:0)
试试这个:
JSONArray driverArray = response.getJSONArray("showDriver");
String IDDriver = "",Driverlogtit="",Driverlogtit="";
for (int z = 0; z < driverArray.length(); z++) {
try {
JSONObject jsDriverArray = driverArray.getJSONObject(z);
if(jsDriverArray.has("IDDriver")){
IDDriver = jsDriverArray.getString("IDDriver");
} if(jsDriverArray.has("Driverlogtit")){
Driverlogtit = jsDriverArray.getString("Driverlogtit");
} if(jsDriverArray.has("Driverlatite")){
Driverlogtit = jsDriverArray.getString("Driverlatite");
}
DriverList.add(new AllDriverLocation(IDDriver,Driverlogtit,Driverlogtit));
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONObject类有一个名为&#34;的方法有&#34;:
http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)
如果此对象具有name的映射,则返回true。映射可能为NULL。
答案 2 :(得分:0)
为了避免应用程序崩溃,您可以添加条件来检查驱动程序数组是否为空?试试这个
JSONArray DriverArray = !data.isNull(getString("showDriver"))?data.getJSONArray(getString("showDriver")) : new JSONArray();