gpt4 book ai didi

java - 异步任务空错误

转载 作者:行者123 更新时间:2023-11-30 04:09:28 25 4
gpt4 key购买 nike

我目前正在处理一个解析 JSON 字符串的异步任务。一旦字符串被解析并且任务从 doInBackground() 移动到 postExecute() 它给了我这个错误:

06-17 20:19:53.612: E/AndroidRuntime(591): FATAL EXCEPTION: main
06-17 20:19:53.612: E/AndroidRuntime(591): java.lang.NumberFormatException: unable to parse 'null' as integer
06-17 20:19:53.612: E/AndroidRuntime(591): at java.lang.Integer.parseInt(Integer.java:356)
06-17 20:19:53.612: E/AndroidRuntime(591): at java.lang.Integer.parseInt(Integer.java:332)
06-17 20:19:53.612: E/AndroidRuntime(591): at stefan.testservice.ParkingActivity$DownloadTask.onPostExecute(ParkingActivity.java:258)
06-17 20:19:53.612: E/AndroidRuntime(591): at android.os.AsyncTask.finish(AsyncTask.java:417)
06-17 20:19:53.612: E/AndroidRuntime(591): at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-17 20:19:53.612: E/AndroidRuntime(591): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-17 20:19:53.612: E/AndroidRuntime(591): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 20:19:53.612: E/AndroidRuntime(591): at android.os.Looper.loop(Looper.java:123)
06-17 20:19:53.612: E/AndroidRuntime(591): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-17 20:19:53.612: E/AndroidRuntime(591): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 20:19:53.612: E/AndroidRuntime(591): at java.lang.reflect.Method.invoke(Method.java:507)
06-17 20:19:53.612: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-17 20:19:53.612: E/AndroidRuntime(591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-17 20:19:53.612: E/AndroidRuntime(591): at dalvik.system.NativeStart.main(Native Method)

它之前工作得很好,突然之间异步任务结束时的“return null”语句似乎出现了一些问题。我似乎找不到解决办法。提前致谢。

我的代码 fragment :

private class DownloadTask extends AsyncTask<String, Void, Object> {
@Override
protected Object doInBackground(String... args) {
Log.i("MyApp", "Background thread starting");

Global.url = "http://10.0.2.2:8000/CarServiceScheduling1/resources/json/product/getpark?degree="+degreeStatus+"&year="+yearStatus+"&distance="+distance+"&devid="+Global.android_id;

try{
statusCode = cg.connection();
}catch (Exception e) {
test_connection = true;
}


if(test_connection == false){

try {
output = cg.stringconverter(Global.data);
cg.parseGson(output);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
return null;
}



protected void onPostExecute(Object result) {

if (ParkingActivity.this.progressDialog != null) {
ParkingActivity.this.progressDialog.dismiss();

if(CheckboxPreference == true && Integer.parseInt(Global.preference1) == 0 && Integer.parseInt(Global.preference2) == 0 && (Global.status.equalsIgnoreCase("No Slots available") || Global.status.equalsIgnoreCase("No"))){


Intent searchSubActivity = new Intent(getBaseContext(),
Nospotsactivity.class);
startActivity(searchSubActivity);
}
else if (CheckboxPreference == true && (Integer.parseInt(Global.preference1) != 0 || Integer.parseInt(Global.preference2) != 0 )){

Intent searchSubActivity = new Intent(getBaseContext(),
Spotsfoundactivity.class);
startActivity(searchSubActivity);
}
else if(CheckboxPreference == false && (Integer.parseInt(Global.preference1) != 0 || Integer.parseInt(Global.preference2) != 0 )){


Intent searchSubActivity = new Intent(getBaseContext(),
Spotsfoundactivity.class);
startActivity(searchSubActivity);
}
if(Integer.parseInt(Global.preference1) == 0 && Integer.parseInt(Global.preference2) == 0){

if(Global.status.equalsIgnoreCase("Hardly Likely")){


Intent searchSubActivity = new Intent(getBaseContext(),
Unlikelyactivity.class);
startActivity(searchSubActivity);
}


else if(Global.status.equalsIgnoreCase("Likely")){


Intent searchSubActivity = new Intent(getBaseContext(),
Likelyactivity.class);
startActivity(searchSubActivity);

}
}
}
}
}
}
}

最佳答案

根据您的堆栈跟踪,Global.preference1Global.preference2 为空(或两者)。在将它们解析为整数之前,您可能需要进行空检查。

关于java - 异步任务空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073724/

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