0

我想做的是在我的应用程序搜索他们的位置时使用异步任务显示进度对话框。找到他们的位置后,它应该显示一个带有一些信息的警报对话框。但是我不是 100% 确定如何做到这一点。这就是我在 LocationChanged.java 中的内容:

@Override
protected Void doInBackground( Void... params ) {
    final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    geocoder = new Geocoder(context, Locale.getDefault());
    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            lat =location.getLatitude();
            lon = location.getLongitude();
            //TextView tv = (TextView) findViewById(R.id.tv3);
            try{
                addresses=(geocoder.getFromLocation(lat, lon, 1));
                if (addresses.size() > 0) {
                    // String address = addresses.get(0).getAddressLine(0);
                    String city = addresses.get(0).getAddressLine(1);
                    String id = city.substring(0,city.length()-5);
                    String state1=String.valueOf(id.charAt(id.length()-3));
                    String state2=String.valueOf(id.charAt(id.length()-2));
                    STATE = state1+state2;
                    //String country = addresses.get(0).getAddressLine(2);
                    //tv.setText(STATE);
                    progressDialog.dismiss();
                    locationManager.removeUpdates(this);
                }
                else {
                    //tv.setText("Oops...");
                }

            }catch(IOException e){
                e.printStackTrace();
            }

        }

这是运行时的logcat:

07-08 17:26:21.456: ERROR/AndroidRuntime(2755): FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:864)
    Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    at android.os.Handler.<init>(Handler.java:121)
    at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:209)
    at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:209)
    at android.location.LocationManager._requestLocationUpdates(LocationManager.java:687)
    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:512)
    at com.nick.whatsthetip.LocationChanged.doInBackground(LocationChanged.java:121)
    at com.nick.whatsthetip.LocationChanged.doInBackground(LocationChanged.java:24)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    ... 5 more
07-08 17:26:21.716: ERROR/EmbeddedLogger(696): App crashed! Process:    com.nick.whatsthetip
07-08 17:26:21.716: ERROR/EmbeddedLogger(696): App crashed! Package: com.nick.whatsthetip v5 (1.1.0)
07-08 17:26:21.987: ERROR/WindowManager(2755): Activity com.nick.whatsthetip.Settings    has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41075198 that was originally added here
    android.view.WindowLeaked: Activity com.nick.whatsthetip.Settings has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41075198 that was originally added here
    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:468)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:419)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:351)
    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:171)
    at android.view.Window$LocalWindowManager.addView(Window.java:558)
    at android.app.Dialog.show(Dialog.java:282)
    at android.app.ProgressDialog.show(ProgressDialog.java:116)
    at android.app.ProgressDialog.show(ProgressDialog.java:99)
    at android.app.ProgressDialog.show(ProgressDialog.java:94)
    at com.nick.whatsthetip.LocationChanged.onPreExecute(LocationChanged.java:49)
    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
    at android.os.AsyncTask.execute(AsyncTask.java:534)
    at com.nick.whatsthetip.Settings$3.onClick(Settings.java:119)
    at android.view.View.performClick(View.java:4102)
4

2 回答 2

2

您应该知道,AsyncTask 的 doInBackground 方法在“Worker”线程上执行,只有 onProgressUpdate 和 onPostExecute 在主线程上执行,因此,尝试修改与 UI 相关的任何内容(如进度对话框......) doInBackground 将导致异常,您要做的是确保从主线程而不是从工作线程调用任何修改 UI 的代码...

问候

于 2013-07-08T21:42:47.503 回答
1

本培训课程:显示位置地址向您展示了如何运行 AsyncTask 来对位置进行反向地理编码。

无需在后台响应位置更新。我建议您从 doInBackground 中取出 onLocationChanged 并将其作为方法添加到 UI 线程中。它是异步的,因此不会导致 ANR。获得位置,执行查找城市、州等的后台任务。培训课程向您展示如何在不使用对话框的情况下放置进度指示器。

于 2013-07-08T22:29:10.297 回答