2012-10-04 31 views
2

時,城市名稱的空指針異常我有點新的BlackBerry OS開發,我的代碼試圖獲取城市的名稱,而使用LocationProvider後通過Criteria參數。當使用位置API與LBS JSR-179和JSR-179擴展

「與JSR 179」

public String CurrentLocation() 
    { 
     try 
     { 
      //LBS(Location Based Service) JSR-179 
      Criteria criteria = new Criteria(); 
      criteria.isAddressInfoRequired(); 
      criteria.setCostAllowed(false); 
      criteria.setAddressInfoRequired(true); 
      criteria.setHorizontalAccuracy(200); 
      criteria.setVerticalAccuracy(200); 

      locationProvider = LocationProvider.getInstance(criteria); 

      location = locationProvider.getLocation(10); 
      cityName = location.getAddressInfo().getField(AddressInfo.CITY); 
     } 
     catch (LocationException le) 
     { 
      new Tracer("CurrentLocation() caught LocationException : " + le.getMessage()); 
      le.printStackTrace(); 
     } 
     catch (InterruptedException ire) 
     { 
      new Tracer("CurrentLocation() caught InterruptedException: " + ire.getMessage()); 
      ire.printStackTrace(); 
     } 
     return cityName; 
    } 

與JSR: 我是根據本link從RIM本身我試圖 「JSR-179」 和 「JSR-179擴展」 我的代碼如下以下-179分機

public String CurrentLocaiton(){ 
     try 
     { 
      BlackBerryCriteria BBCriteria = new BlackBerryCriteria(); 
      BBCriteria.setAddressInfoRequired(ProvideAddressInfoTExt); 
      BBCriteria.setAltitudeRequired(true); 
      BBCriteria.setSatelliteInfoRequired(true, true); 
      BBCriteria.setCostAllowed(true); 
      BBCriteria.setPreferredPowerConsumption(BlackBerryCriteria.POWER_USAGE_HIGH); //testing for high power 

      BlackBerryLocationProvider bbLocationProvider 
               = (BlackBerryLocationProvider) 
                BlackBerryLocationProvider.getInstance(BBCriteria); 
      location = bbLocationProvider.getLocation(9000); 
      QualifiedCoordinates qualCoor = location.getQualifiedCoordinates(); 
      cityName = location.getAddressInfo().getField(AddressInfo.CITY); 
     } 
     catch(LocationException le) 
     { 
      le.printStackTrace(); 
      System.out.println(le.getMessage()); 
     } 
     catch(InterruptedException ie) 
     { 
      ie.printStackTrace(); 
      System.out.println(ie.getMessage()); 
     } 
     catch(NullPointerException npe) 
     { 
      npe.printStackTrace(); 
      cityName = "Caught Null Pointer"; 
      Dialog.alert(npe.getMessage()); 
     } 
     return cityName; 
    } 

我錯過了什麼或有什麼問題我一直在這個問題上抓了好幾個小時。 我試圖趕上從這個代碼片斷以及Null Pointer Exception,如果從「靜態無效的主要」然後Uncaught Exception: pushModalScreen called by a non-event thread

 .... 
     try{ 
      String location = cellLocation.CurrentLocation(); 
      LabelField towerlocationText = new LabelField(towerString + location, 
           LabelField.FOCUSABLE|LabelField.DEFAULT_POSITION); 
      add(towerlocationText); 
     } 
     catch(NullPointerException npe) 
     { 
      npe.printStackTrace(); 
      System.out.println(npe.getMessage()); 
      Dialog.alert(npe.getMessage() + "" + "AddresInfo/LocationProvider/Locaiton gave null pointer exception"); 
     } 
     ... 

追趕試圖在BB-9700 BB-9790設備以及對模擬器BB-9900

感謝,

+0

請分享堆棧跟蹤 –

+1

'Dialog.alert()'不是記錄異常的好方法。對於你的情況,它會拋出「由非事件線程調用的pushModalScreen」。使用設備存儲器/ SD卡上的Logger類或文本文件記錄所需信息。關於你的代碼,似乎cellLocation.CurrentLocation()返回空值。這是你NPE的原因。在實際設備上運行此代碼時,請確保您有GPS sattelites可用,並使用Logger類記錄您需要製作的每個「可疑」步驟。要使用鍵盤查看設備上的日誌,請按CTRL,按住並按下鍵盤上的「LGLG」。 – 2012-10-05 05:13:38

回答

1
use 
Either :- 
/***/ 
     UiApplication.getUiApplication().invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       Dialog.alert("Hello"); 
      } 
     }); 
/***/ 
or  
     synchronized (UiApplication.getEventLock()) 
     { 
        Dialog.alert("Hello"); 
     } 

無論你試圖顯示在非事件線程對話框,如果你需要一個事件鎖。 您可以使用上述方法使事件鎖 閱讀article瞭解更多信息。