我是新手,所以请多多包涵。
我有一个异步任务,它从网络服务中提取离我最近的位置。它在大于 2.1 的较新的 android 设备上运行良好。我有一个 2.1 的 HTC hero,每次按下按钮查找数据时它都会崩溃。
这是我拥有的代码。第一部分从设备获取 lat 和 long 并将其发送到我在服务器上创建的 php 脚本。
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
//String response = "";
for (String url : urls) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("lat", lat));
nameValuePairs.add(new BasicNameValuePair("long", longit));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8);
httppost.setEntity(ent);
HttpResponse responsePOST = httpclient.execute(httppost);
responseBody = EntityUtils.toString(responsePOST.getEntity());
Log.i("YAhh", responseBody);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
return responseBody;
}
这部分将数据发送到另一个活动以进行解析并显示在列表视图中。
@Override
protected void onPostExecute(String result) {
Log.i("returndata",responseBody);
/* Intent intent = new Intent(HomeActivity.this,
locationActivity.class);
Bundle bundle = new Bundle();
bundle.putString("returnBody", responseBody);
bundle.putString("latit", lat);
bundle.putString("longit" + "", longit);
intent.putExtras(bundle);
startActivity(intent);*/
}
}
这是我收到的错误,据我所知没有返回数据,因此空值导致错误。
08-02 15:17:13.848: E/AndroidRuntime(1117): Uncaught handler: thread main exiting due to uncaught exception
08-02 15:17:13.968: E/AndroidRuntime(1117): java.lang.NullPointerException: println needs a message
08-02 15:17:13.968: E/AndroidRuntime(1117): at android.util.Log.println(Native Method)
08-02 15:17:13.968: E/AndroidRuntime(1117): at android.util.Log.i(Log.java:136)
08-02 15:17:13.968: E/AndroidRuntime(1117): at com.mlgw.mlgwapp.HomeActivity$DownloadWebPageTask.onPostExecute(HomeActivity.java:278)
08-02 15:17:13.968: E/AndroidRuntime(1117): at com.mlgw.mlgwapp.HomeActivity$DownloadWebPageTask.onPostExecute(HomeActivity.java:1)
08-02 15:17:13.968: E/AndroidRuntime(1117): at android.os.AsyncTask.finish(AsyncTask.java:417)
08-02 15:17:13.968: E/AndroidRuntime(1117): at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-02 15:17:13.968: E/AndroidRuntime(1117): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-02 15:17:13.968: E/AndroidRuntime(1117): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 15:17:13.968: E/AndroidRuntime(1117): at android.os.Looper.loop(Looper.java:123)
08-02 15:17:13.968: E/AndroidRuntime(1117): at android.app.ActivityThread.main(ActivityThread.java:4595)
08-02 15:17:13.968: E/AndroidRuntime(1117): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 15:17:13.968: E/AndroidRuntime(1117): at java.lang.reflect.Method.invoke(Method.java:521)
08-02 15:17:13.968: E/AndroidRuntime(1117): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-02 15:17:13.968: E/AndroidRuntime(1117): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-02 15:17:13.968: E/AndroidRuntime(1117): at dalvik.system.NativeStart.main(Native Method)
有人知道为什么这不适用于 2.1 吗?任何帮助,将不胜感激。