Geocoder获取位置:无法在未调用Looper.prepare()的线程内创建处理程序

时间:2014-05-23 12:06:38

标签: android android-asynctask geolocation android-location

我想从city name对象中获取location。我使用async task在主要活动的android.location.GeoCoder中执行此操作。

我就是这样做的:

public void getCityName(final Location location,
            final OnGeocoderFinishedListener listener) {
        new AsyncTask<Void, Integer, List<Address>>() {
            @Override
            protected List<Address> doInBackground(Void... arg0) {
                Geocoder coder = new Geocoder(context, Locale.ENGLISH);
                List<Address> results = null;
                try {
                    results = coder.getFromLocation(location.getLatitude(),
                            location.getLongitude(), 1);
                } catch (IOException e) {
                    // nothing
                }
                return results;
            }

            @Override
            protected void onPostExecute(List<Address> results) {
                if (results != null && listener != null) {
                    listener.onFinished(results);
                }
            }
        }.execute();
    }

    public abstract class OnGeocoderFinishedListener {
        public abstract void onFinished(List<Address> results);
    }

这是我得到的错误:

05-23 12:30:08.395: E/AndroidRuntime(24099): FATAL EXCEPTION: Timer-0
05-23 12:30:08.395: E/AndroidRuntime(24099): java.lang.ExceptionInInitializerError
05-23 12:30:08.395: E/AndroidRuntime(24099):    at orient.tvguide.main.MainActivity.getCityName(MainActivity.java:573)
05-23 12:30:08.395: E/AndroidRuntime(24099):    at orient.tvguide.main.MainActivity$1.gotLocation(MainActivity.java:289)
05-23 12:30:08.395: E/AndroidRuntime(24099):    at orient.tvguide.location.MyLocation$GetLastLocation.run(MyLocation.java:116)
05-23 12:30:08.395: E/AndroidRuntime(24099):    at java.util.Timer$TimerImpl.run(Timer.java:284)
05-23 12:30:08.395: E/AndroidRuntime(24099): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-23 12:30:08.395: E/AndroidRuntime(24099):    at android.os.Handler.<init>(Handler.java:121)
05-23 12:30:08.395: E/AndroidRuntime(24099):    at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
05-23 12:30:08.395: E/AndroidRuntime(24099):    at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
05-23 12:30:08.395: E/AndroidRuntime(24099):    at android.os.AsyncTask.<clinit>(AsyncTask.java:190)
05-23 12:30:08.395: E/AndroidRuntime(24099):    ... 4 more

我还尝试在async上运行runOnUiThread,但问题仍然存在。

public void getCityName(final Location location,
            final OnGeocoderFinishedListener listener) {

        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                new AsyncTask<Void, Integer, List<Address>>() {
                    @Override
                    protected List<Address> doInBackground(Void... arg0) {
                        Geocoder coder = new Geocoder(context, Locale.ENGLISH);
                        List<Address> results = null;
                        try {
                            results = coder.getFromLocation(location.getLatitude(),
                                    location.getLongitude(), 1);
                        } catch (IOException e) {
                            // nothing
                        }
                        return results;
                    }

                    @Override
                    protected void onPostExecute(List<Address> results) {
                        if (results != null && listener != null) {
                            listener.onFinished(results);
                        }
                    }
                }.execute();
            }
        });
    }

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

onBackground()

之前的Geocoder coder = new Geocoder();内添加这段代码
try {
    Looper.prepare();
} catch (Exception e) {
    //Allready called
}

告诉我你的结果:)