当用户中断到数据馈送的传输时,我的 Android 应用程序会null
强制关闭。或者有时如果互联网速度很慢而且似乎停滞不前,我会得到null
这是我处理http代码的方式:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
然后在onPostExecute
, AsyncTask
null 在这里返回,如下面的 LogCat 所示:
adapter = new ReviewAdapter(getActivity(), r, tf); // Line 1164
我想r
是什么null
杀死了它?(tf
是我传入的TrueType字体)
这是r
代码:
for (String i : reviews) {
r[index] = new Review(reviews.get(index),
datelist.get(index), userlist.get(index),
itemlist.get(index), ratings.get(index),
helpfulCount.get(index), chatCounts.get(index),
catlist.get(index), titlelist.get(index));
index++;
}
这些ArrayLists
是通过JSON
Parsing fromPHP
等填充的。
所以我的问题是:我如何普遍处理这个问题?有没有办法避免null?或者我怎样才能优雅地抛出错误而不是强制关闭?
这是Logcat:
02-19 12:15:12.012: E/AndroidRuntime(32116): FATAL EXCEPTION: main
02-19 12:15:12.012: E/AndroidRuntime(32116): java.lang.NullPointerException
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.---.---.ReviewAdapter.<init>(ReviewAdapter.java:42)
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.---.---.RateFragmentActivity$ReviewFragment$ReviewTask.onPostExecute(RateFragmentActivity.java:1164)
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.---.---.RateFragmentActivity$ReviewFragment$ReviewTask.onPostExecute(RateFragmentActivity.java:1)
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.Looper.loop(Looper.java:137)
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.app.ActivityThread.main(ActivityThread.java:5193)
02-19 12:15:12.012: E/AndroidRuntime(32116): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 12:15:12.012: E/AndroidRuntime(32116): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-19 12:15:12.012: E/AndroidRuntime(32116): at dalvik.system.NativeStart.main(Native Method)