我有這個Asynctask應該計算用戶的位置。 Asynctask中的代碼在Asynctask之外工作良好,所以我知道我的錯誤在於安排Asynctask,這是我很不熟悉的。Asynctask崩潰,找不到原因
的AsyncTask由該行一個onClick方法衝了進去:
new GetCurrentCity().execute();
這裏是GetCurrentCity本身:
private class GetCurrentCity extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... args) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress = (returnedAddress.getAddressLine(i)).toString();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
return strReturnedAddress;
}
protected void onPostExecute(String useless2){
test.setText(strReturnedAddress);
}
}
正如我剛纔所說,我用不同的線程總新手,所以請對不起,如果這是一個非常簡單的問題。
此外,我仍然嚴重的參數掙扎,所以我覺得問題是與他們的;然而,在我來這裏問問之前,我已經嘗試了所有我能想到的東西。
預先感謝您,祝您有美好的一天!
編輯:這是當前的logcat
07-10 20:24:08.175: D/OpenGLRenderer(15827): Enabling debug mode 0
07-10 20:24:08.435: I/TextToSpeech(15827): Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.GoogleTTSService}
07-10 20:24:13.460: W/dalvikvm(15827): threadid=13: thread exiting with uncaught exception (group=0x41471930)
07-10 20:24:13.465: E/AndroidRuntime(15827): FATAL EXCEPTION: AsyncTask #3
07-10 20:24:13.465: E/AndroidRuntime(15827): java.lang.RuntimeException: An error occured while executing doInBackground()
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-10 20:24:13.465: E/AndroidRuntime(15827): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-10 20:24:13.465: E/AndroidRuntime(15827): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-10 20:24:13.465: E/AndroidRuntime(15827): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-10 20:24:13.465: E/AndroidRuntime(15827): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-10 20:24:13.465: E/AndroidRuntime(15827): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-10 20:24:13.465: E/AndroidRuntime(15827): at java.lang.Thread.run(Thread.java:856)
07-10 20:24:13.465: E/AndroidRuntime(15827): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.os.Handler.<init>(Handler.java:197)
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.os.Handler.<init>(Handler.java:111)
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:197)
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:197)
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.location.LocationManager.wrapListener(LocationManager.java:820)
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:833)
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:430)
07-10 20:24:13.465: E/AndroidRuntime(15827): at com.example.realapptest1.MainActivity$GetCurrentCity.doInBackground(MainActivity.java:306)
07-10 20:24:13.465: E/AndroidRuntime(15827): at com.example.realapptest1.MainActivity$GetCurrentCity.doInBackground(MainActivity.java:1)
07-10 20:24:13.465: E/AndroidRuntime(15827): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-10 20:24:13.465: E/AndroidRuntime(15827): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-10 20:24:13.465: E/AndroidRuntime(15827): ... 4 more
發佈錯誤日誌,這個錯誤 - 您當前正在運行的這個與沒有參數 – Rarw
有一件事幫助我理解AsyncTask是爲了確保我在'doInBackground','onProgressUpdate'和'onPostExecute'上放置'@ Override'聲明並觀察發生了什麼,因爲我改變了'extends'部分中的類型。你會看到他們都是如何相關的。 –