在从 Android MainActivity 中的地图例程获取 gps 坐标后,我正在尝试运行异步任务。异步任务是根据 gps 坐标执行 httpurlconnection 从我的服务器获取数据。但是,在我的调试过程中观察时,异步任务 GetRestaurant() 永远不会被调用。我应该提到,在调试器中,循环器例程被一遍又一遍地调用。在调试中,我注意到程序执行了 GetRestaurant 指令,但之后没有任何反应。班级永远达不到。地图确实会显示出来,仅此而已。谁能从下面的代码中看出为什么我的异步任务类 GetRestaurant 永远不会被执行?
locationManager.requestLocationUpdates(provider, 3600, 1000, new LocationListener() {
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onLocationChanged(Location loc) {
if(loc != null) {
lat = (float) (loc.getLatitude());
lng = (float) (loc.getLongitude());
String latLongString = "Lat:" + lat + "\nLong:" + lng;
myLocationText.setText("Your current position is:\n" + latLongString);
int latit = (int) (loc.getLatitude() * 1E6);
int longit = (int) (loc.getLongitude() * 1E6);
point = new GeoPoint(latit, longit);
mapController.animateTo(point);
mapController.setCenter(point);
mapController.setZoom(15);
String addr=getAddress(point);
TextView addressView = (TextView) findViewById(R.id.address_view);
addressView.setText(addr);
MyOverlay mapOverlay = new MyOverlay();
List<Overlay> overlayList=mapView.getOverlays();
overlayList.clear();
overlayList.add(mapOverlay);
new GetRestaurant().execute();
}
}
});
}
class GetRestaurant extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void...parameters ) {
// TODO Auto-generated method stub
String thislat = ((String.valueOf(lat)));
String thislng = ((String.valueOf(lng)));
String[] result = null;
URL url;
try {
String query = "lat=" + thislat + "&lng=" + thislng + "&uid=&vegkey=1";
url = new URL("http://10.0.2.2:8000/xxxxxxx/webservice/frontpage1.php?" + query);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
int x=0;
while ((line = br.readLine()) != null) {
// sb.(line);
result[x] = line;
x++;
}
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
System.out.println(e.toString());
}
return result;
}
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(String[] result) {
restaurants = result;
final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(null, android.R.layout.simple_list_item_1, restaurants);
restaurant_list.setAdapter(arrayAdpt);
mapView.postInvalidate();
}
}
这是我的错误日志:我注意到此错误:“无法在未调用 Looper.prepare() 的线程内创建处理程序。” 这听起来很可疑。
03-08 23:53:27.846: E/AndroidRuntime(570): FATAL EXCEPTION: AsyncTask #1
03-08 23:53:27.846: E/AndroidRuntime(570): java.lang.RuntimeException: An error occured while executing doInBackground()
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.lang.Thread.run(Thread.java:856)
03-08 23:53:27.846: E/AndroidRuntime(570): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.Handler.<init>(Handler.java:197)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.Handler.<init>(Handler.java:111)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.widget.Toast$TN.<init>(Toast.java:324)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.widget.Toast.<init>(Toast.java:91)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.widget.Toast.makeText(Toast.java:238)
03-08 23:53:27.846: E/AndroidRuntime(570): at com.example.herb4.MainActivity$GetRestaurant.doInBackground(MainActivity.java:159)
03-08 23:53:27.846: E/AndroidRuntime(570): at com.example.herb4.MainActivity$GetRestaurant.doInBackground(MainActivity.java:1)
03-08 23:53:27.846: E/AndroidRuntime(570): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-08 23:53:27.846: E/AndroidRuntime(570): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-08 23:53:27.846: E/AndroidRuntime(570): ... 4 more
好的。我将代码更改为在 doInBackground 末尾读取 'return (String[]) result.toArray()。现在我收到以下错误:'03-09 03:24:18.796: E/AndroidRuntime(4726): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[ ]'。我认为错误发生在 onPostExecute 具有以下代码“
@Override
protected void onPostExecute(String[] result) {
// restaurants = result;
final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, result);
restaurant_list.setAdapter(arrayAdpt);
mapView.postInvalidate();
}
如果我可以在调试器中跟踪程序,这会容易得多,但在我到达问题区域之前它就退出了。