2011-05-13 36 views
1

最近,我的Android項目從Google Maps切換到ESRI ArcGIS,目前我正處於轉換過程中。這是我以後面臨的一個小問題。ArcGIS for Android LocationService異常

無論何時啓用內置位置服務,以下代碼都會拋出NullPointerException。而且,LocationService.start()調用是否位於try-catch塊中或在另一個線程中運行都無關緊要:應用程序只是崩潰。

這裏是Activity類的一個片段:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.map_esri); 
    map = (MapView) findViewById(R.id.map); 
    map.setExtent(boink); 
    map.setOnStatusChangedListener(new OnStatusChangedListener() { 
     private static final long serialVersionUID = 1L; 
     @Override 
     public void onStatusChanged(View source, STATUS status) { 
      if(status == STATUS.INITIALIZED) { 
       //set up location service 
       /* 
       * The following block throws exception and the application crashes. 
       * And it doesn't matter whether I run it from another thread or in a try-catch block! 
       */ 
       new Handler().post(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          locationService = map.getLocationService(); 
          locationService.setLocationListener(MapEsri.this); 
//       locationService.setAccuracyCircleOn(true); 
//       locationService.setBearing(true); 
//       locationService.setAutoPan(true); 
          locationService.start(); 
         } 
         catch(Exception e) { 
          e.printStackTrace(); 
         } 
        } 
       }); 
      } 
     } 
    }); 
} 

這是它拋出異常:

05-13 10:08:55.580: ERROR/AndroidRuntime(28355): java.lang.NullPointerException 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at com.esri.core.geometry.Envelope.<init>(Unknown Source) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at com.esri.core.map.LayerModel.setExtent(Unknown Source) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at com.esri.android.map.LayerView.onSizeChanged(Unknown Source) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.view.View.setFrame(View.java:7123) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.view.View.layout(View.java:7050) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at com.esri.android.map.MapView.onLayout(Unknown Source) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.view.View.layout(View.java:7056) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.view.View.layout(View.java:7056) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.view.View.layout(View.java:7056) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1049) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1744) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.os.Looper.loop(Looper.java:143) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at android.app.ActivityThread.main(ActivityThread.java:5068) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at java.lang.reflect.Method.invoke(Method.java:521) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-13 10:08:55.580: ERROR/AndroidRuntime(28355):  at dalvik.system.NativeStart.main(Native Method) 

我該怎麼辦錯了嗎?

回答

0

這可能有點晚,但狀態可能來自另一個來源。 因此,映射是當其他東西被初始化時拋出null異常的東西。

if(source == map && status == STATUS.INITIALIZED) { 
        //set up location service 
        /* 
        * The following block throws exception and the application crashes. 
        * And it doesn't matter whether I run it from another thread or in a try-catch block! 
        */ 
        new Handler().post(new Runnable() { 
         @Override 
         public void run() { 
          try { 
           locationService = map.getLocationService(); 
           locationService.setLocationListener(MapEsri.this); 
    //       locationService.setAccuracyCircleOn(true); 
    //       locationService.setBearing(true); 
    //       locationService.setAutoPan(true); 
           locationService.start(); 
          } 
          catch(Exception e) { 
           e.printStackTrace(); 
          } 
         } 
        }); 
       } 
+0

您必須爲位置創建自己的列表您是否綁定了任何監聽器? locationService.setLocationListener(MapEsri.this); – 2014-04-30 07:05:31