【发布时间】:2014-09-28 08:52:00
【问题描述】:
有问题的代码:
final GenericAsyncTask task = new GenericAsyncTask();
task.background = new Runnable() {
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(GCMIntentService.this.host);
String addremove = "add";
if(register == false) addremove = "remove";
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("cmd", addremove));
nameValuePairs.add(new BasicNameValuePair("subscription_key", SUBSCRIPTION_KEY)); // unique per app
nameValuePairs.add(new BasicNameValuePair("token", str));
nameValuePairs.add(new BasicNameValuePair("os_family", "android"));
if(addremove.equals("remove")) nameValuePairs.add(new BasicNameValuePair("hard", "" + hard));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(entity);
Log.i(LCHApplication.TAG, "Name Value Pairs: " + nameValuePairs.toString());
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
task.result = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
};
task.callback = new Runnable() {
public void run() {
Log.i(LCHApplication.TAG, "registration response: " + task.result.toString());
}
};
task.execute();
这在大多数情况下都非常有效。然而,有时task.result.toString(); 有时是NULL,它会抛出java.lang.NullPointerException。我想保留这个崩溃,因为我希望它在 crashlytics 中报告,这样我就可以看到它影响了多少人。如果我使用 try/catch 我可以抛出什么来确保它不是致命的崩溃呢?这样它仍然会向 crashlytics 报告,但不会杀死应用程序。
【问题讨论】:
标签: java android nullpointerexception crash fatal-error