2

我想从文件中加载坐标并在异步任务中将形状添加到我的地图中。但我收到错误,我不知道为什么。这是我的代码:

   private class shpLoading extends AsyncTask<GoogleMap, Void, String> {
          ProgressDialog dialog;

          @Override
          protected String doInBackground(GoogleMap... params) {                  
                ShpReader shpRead = new ShpReader();
                GoogleMap map = params[0];
                try {
                    shpRead.reading();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvalidShapeFileException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                for(LatLng a : shpRead.points()) 
                    map.addMarker(new MarkerOptions()
                    .position(a)
                    .draggable(false));

                int i = 0;
                for(List<LatLng> a: shpRead.lines()){
                    map.addPolyline(new PolylineOptions()
                    .addAll(a)
                    .width(3)
                    .color(Color.RED)); 

                }

                for(List<LatLng> a: shpRead.polygons()){
                    map.addPolygon(new PolygonOptions()
                    .addAll(a)
                    .strokeWidth(3)
                    .strokeColor(Color.RED)
                    .fillColor(0x3F00FF00));

                }
                return "Done";
          } 
              @Override
          protected void onPreExecute() {
                  dialog = new ProgressDialog(Measuring.this);        
                  dialog.setMessage("Kraunama...");
                  dialog.setIndeterminate(true);
                  dialog.setCancelable(false);
                  dialog.show();
          }

          @Override
          protected void onProgressUpdate(Void... values) {
          }

          @Override
          protected void onPostExecute(String result) {               
                dialog.dismiss();
          }                      
   }   

这就是我执行代码的方式:

shpLoading load = new shpLoading();
load.execute(mMap);  

mMap 是 GoogleMap 变量。如果我将所有doInBackground代码添加到onPostExecute方法并将变量更改map为全局mMapeveything 正在工作,但我想在后台执行此任务并同时显示加载对话框。

这是我的 LogCat:

 08-26 11:10:52.368: E/AndroidRuntime(11099): FATAL EXCEPTION:
 AsyncTask #1 
 08-26 11:10:52.368: E/AndroidRuntime(11099):
 java.lang.RuntimeException: An error occured while executing
 doInBackground() 
 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 android.os.AsyncTask$3.done(AsyncTask.java:299) 08-26 11:10:52.368:
 E/AndroidRuntime(11099):   at
 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 java.util.concurrent.FutureTask.run(FutureTask.java:137) 08-26
 11:10:52.368: E/AndroidRuntime(11099):     at
 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 08-26
 11:10:52.368: E/AndroidRuntime(11099):     at
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 java.lang.Thread.run(Thread.java:856) 08-26 11:10:52.368:
 E/AndroidRuntime(11099): Caused by: java.lang.IllegalStateException:
 Not on the main thread 08-26 11:10:52.368: E/AndroidRuntime(11099):
    at maps.ar.p.b(Unknown Source) 08-26 11:10:52.368:
 E/AndroidRuntime(11099):   at maps.al.g.b(Unknown Source) 08-26
 11:10:52.368: E/AndroidRuntime(11099):     at maps.ag.an.a(Unknown
 Source) 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 bkw.onTransact(SourceFile:137) 08-26 11:10:52.368:
 E/AndroidRuntime(11099):   at
 android.os.Binder.transact(Binder.java:326) 08-26 11:10:52.368:
 E/AndroidRuntime(11099):   at
 com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addPolyline(Unknown
 Source) 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 com.es.map.Measuring$shpLoading.doInBackground(Measuring.java:1165)
 08-26 11:10:52.368: E/AndroidRuntime(11099):   at
 com.es.map.Measuring$shpLoading.doInBackground(Measuring.java:1) 08-26
 11:10:52.368: E/AndroidRuntime(11099):     at
 android.os.AsyncTask$2.call(AsyncTask.java:287) 08-26 11:10:52.368:
 E/AndroidRuntime(11099):   at
 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 08-26 11:10:52.368: E/AndroidRuntime(11099):   ... 5 more 08-26
 11:11:00.453: E/WindowManager(11099): Activity com.es.map.Measuring
 has leaked window
 com.android.internal.policy.impl.PhoneWindow$DecorView@42229e30 that
 was originally added here 08-26 11:11:00.453: E/WindowManager(11099):
 android.view.WindowLeaked: Activity com.es.map.Measuring has leaked
 window com.android.internal.policy.impl.PhoneWindow$DecorView@42229e30
 that was originally added here 08-26 11:11:00.453:
 E/WindowManager(11099):    at
 android.view.ViewRootImpl.<init(ViewRootImpl.java:403) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 android.view.Window$LocalWindowManager.addView(Window.java:554) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 android.app.Dialog.show(Dialog.java:277) 08-26 11:11:00.453:
 E/WindowManager(11099):    at
 com.es.map.Measuring$shpLoading.onPreExecute(Measuring.java:1195)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 android.os.AsyncTask.execute(AsyncTask.java:534) 08-26 11:11:00.453:
 E/WindowManager(11099):    at
 com.es.map.Measuring.onOptionsItemSelected(Measuring.java:566) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 android.app.Activity.onMenuItemSelected(Activity.java:2629) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:366)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 android.view.View$PerformClick.run(View.java:17298) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 android.os.Handler.handleCallback(Handler.java:615) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 android.os.Handler.dispatchMessage(Handler.java:92) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 android.os.Looper.loop(Looper.java:137) 08-26 11:11:00.453:
 E/WindowManager(11099):    at
 android.app.ActivityThread.main(ActivityThread.java:4921) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 java.lang.reflect.Method.invokeNative(Native Method) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 java.lang.reflect.Method.invoke(Method.java:511) 08-26 11:11:00.453:
 E/WindowManager(11099):    at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 08-26 11:11:00.453: E/WindowManager(11099):    at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 08-26
 11:11:00.453: E/WindowManager(11099):  at
 dalvik.system.NativeStart.main(Native Method)

也许这不是从文件加载坐标并将它们添加到地图的好方法?也许有更好的方法来做到这一点?

4

2 回答 2

3

抱歉,您不能从后台线程更改 UI 组件,您必须在 UIThread 中编写地图操作

您现有代码中的解决方案如下:

private Handler mHandler = new Handler();

    private class shpLoading extends AsyncTask<GoogleMap, Void, String> {
        ProgressDialog dialog;

        @Override
        protected String doInBackground(GoogleMap... params) {                  
              ShpReader shpRead = new ShpReader();
              GoogleMap map = params[0];
              try {
                  shpRead.reading();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (InvalidShapeFileException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }

              mHandler.post(new Runnable() {
                public void run() {
                    for(LatLng a : shpRead.points()) 
                        map.addMarker(new MarkerOptions()
                        .position(a)
                        .draggable(false));

                    int i = 0;
                    for(List<LatLng> a: shpRead.lines()){
                        map.addPolyline(new PolylineOptions()
                        .addAll(a)
                        .width(3)
                        .color(Color.RED)); 

                    }

                    for(List<LatLng> a: shpRead.polygons()){
                        map.addPolygon(new PolygonOptions()
                        .addAll(a)
                        .strokeWidth(3)
                        .strokeColor(Color.RED)
                        .fillColor(0x3F00FF00));

                    }
                }
            });
              return "Done";
        } 
            @Override
        protected void onPreExecute() {
                dialog = new ProgressDialog(Measuring.this);        
                dialog.setMessage("Kraunama...");
                dialog.setIndeterminate(true);
                dialog.setCancelable(false);
                dialog.show();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }

        @Override
        protected void onPostExecute(String result) {               
              dialog.dismiss();
        }                      
 }   
于 2013-08-26T08:19:58.950 回答
0

您不能在后台进行 ui 操作,map.add 应该发生在进度更改中......只需调用报告进度并在那里进行地图绘制。

于 2013-08-26T08:18:27.530 回答