Asynctask崩溃,找不到原因

时间:2013-07-10 17:03:35

标签: android android-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

1 个答案:

答案 0 :(得分:0)

  

我仍然在严格挣扎参数,所以我觉得问题出在他们身上

params doInBackground()来自execute()。所以无论你把它放在哪个方法中,都会被发送到doInBackground()。您可以在其中放置StringString vargs或将doInBackground()更改为不接受String

     protected String doInBackground(Void... args)

然后将您的AsyncTask声明更改为

 private class GetCurrentCity extends AsyncTask<Void, Void, String>

doInBackground()将其值恢复为onPostExecute(),因为您似乎正在执行此操作,但您没有使用已发送的值,因此没有理由return

onProgressUpdate()params拨打publishProgress() doInBackground()来执行更新ProgressBar之类的操作。

如果没有看到您的logcat,很难准确说出您遇到了什么问题,但这就是您将params发送到AsyncTask的方式

AsyncTask Docs