2012-10-04 32 views
1

我想要做的是有一個地圖,包含ButtonField字段的點擊有兩個標準的開放,用戶必須指定位置,然後圖像必須被添加到該位置,否則該圖像必須被添加到用戶當前位置。錯誤的位置:的getLocation()方法不能從事件線程調用

我有是將兩個指標分析到如果問題在一個線程/新線程,甚至在FieldChangeListener聲明。

總是收到的錯誤是:

在錯誤的位置:javax.microedition.location.LocationException: 的getLocation()方法不能CAL [0.0]從事件線程[0.0]

導致

在錯誤的位置:的getLocation()方法不能從事件 線程調用

我的代碼:

FieldChangeListener Listener = new FieldChangeListener() { 
    public void fieldChanged(Field field, int context) { 
     ButtonField buttonClicked = (ButtonField) field; 
     if ((buttonClicked.getLabel()).equals("Push")) { 
      CustomMapField mMapField; 
      Coordinates mCoordinates; 
      BlackBerryCriteria blackBerryCriteria = null; 
      BlackBerryLocation blackBerryLocation = null; 
      BlackBerryLocationProvider blackBerryLocationProvider = null; 
      double Doublelat = 0.0; 
      double Doublelng = 0.0; 
      blackBerryCriteria = new BlackBerryCriteria(); 
      if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_CELLSITE)){ 
        blackBerryCriteria.setMode(GPSInfo.GPS_MODE_CELLSITE); 
      }else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_ASSIST)){ 
       blackBerryCriteria.setMode(GPSInfo.GPS_MODE_ASSIST); 
      }else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_AUTONOMOUS)){ 
       blackBerryCriteria.setMode(GPSInfo.GPS_MODE_AUTONOMOUS); 
      }else{ 
       blackBerryCriteria.setCostAllowed(true); 
       blackBerryCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW); 
      } try { 
       blackBerryLocationProvider = (BlackBerryLocationProvider) BlackBerryLocationProvider.getInstance(blackBerryCriteria); 
       blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60); 
       QualifiedCoordinates qualifiedCoordinates = blackBerryLocation.getQualifiedCoordinates(); 

       Doublelat = qualifiedCoordinates.getLatitude(); 
       Doublelng = qualifiedCoordinates.getLongitude(); 
       mCoordinates = new Coordinates(Doublelat, Doublelng, 0); 
       mMapField = new CustomMapField(); 
       mMapField.mIcon = Bitmap.getBitmapResource("coin_silver.png"); 
       mMapField.moveTo(mCoordinates); 
       mMapField.setZoom(1); 
       add(mMapField); 
      }catch(Exception e){ 
       System.out.println("Debug 5"); 
       System.out.println("Error in location :"+e.toString()); 
       System.out.println("Error in location :"+e.getMessage()); 
      } 
     } 
    } 
}; 

public class CustomMapField extends MapField { 
    Bitmap mIcon; 
    XYRect mDest; 

    public void moveTo(Coordinates coordinates) { 
     super.moveTo(coordinates); 
     mDest = null; 
    } 

    protected void paint(Graphics graphics) { 
     super.paint(graphics); 
     if (null != mIcon) { 
      if (null == mDest) { 
       XYPoint fieldOut = new XYPoint(); 
       convertWorldToField(getCoordinates(), fieldOut); 
       int imgW = mIcon.getWidth(); 
       int imgH = mIcon.getHeight(); 
       mDest = new XYRect(fieldOut.x - imgW/2, 
       fieldOut.y - imgH, imgW, imgH); 
      } 
      graphics.drawBitmap(mDest, mIcon, 0, 0); 
     } 
    } 
} 

錯誤與下面的行add(mMapField);

Doublelat = qualifiedCoordinates.getLatitude(); 
    Doublelng = qualifiedCoordinates.getLongitude(); 
    mCoordinates = new Coordinates(Doublelat, Doublelng, 0); 
    mMapField = new CustomMapField(); 
    mMapField.mIcon=Bitmap.getBitmapResource("coin_silver.png"); 
    mMapField.moveTo(mCoordinates); 
    mMapField.setZoom(1); 
    add(mMapField); 

    /*MapView mapView = new MapView(); 
    mapView.setLatitude(finalintlat); 
    mapView.setLongitude(finalintlng); 
    mapView.setZoom(10); 
    MapsArguments mapsArgs = new MapsArguments(mapView); 
    Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, mapsArgs); 

請詳細告訴我具體怎麼做,請舉一個例子,我不能understan如何爲「mMapField」是一個自定義的MapField可及「MapView類」是一類MapView的(請參見上面我的代碼片段)

請幫助急用!

謝謝

+0

只要讓我知道,你的應用程序沒有獲取位置時響應..? –

回答

0

你應該在的地方add(mMapField);

+0

@ Anzy_代碼獲取經度和緯度,這就是我放置所有東西時的一個問題。如果我把所有東西都拿出來,只使用代碼來獲得「我的位置」(使用主屏幕中的代碼),那麼它完美地工作。 – Nequita

+0

@Coral Doe,請查看我在我的帖子中所做的更改,比您的 – Nequita

+1

解決此問題的正確方法是在我的代碼中添加以下內容:'synchronized(UiApplication.getEventLock())mainVFM。添加(mMapField); \t}' – Nequita

2

獲得一個位置使用Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments(mMapField));是一個耗時的任務,它可以採取即使有良好的衛星可見度高達1分鐘,雖然新的漿果首次修復時間(TTFF)大大提高。

耗時的任務(如打開連接或獲取修補程序)不應在事件線程中執行,因爲此線程必須響應用戶事件,並且如果您使用它,則GUI會變得非常慢。並且在fieldChanged內運行的所有東西都在事件線程中運行。因此,RIM在其新的BlackBerryLocationProvider中實施了線程檢測並引發異常,這是一件好事,現在您已經意識到了糟糕的設計並可以採取糾正措施。

您有幾種選擇來獲得異步修復:

  1. 使用LocationListener
  2. 產生一個新的線程。
  3. Preemtively獲取定位你需要它(或定期)前不久,那麼你就必須很快可以按下按鈕時,(無論是從檢索它的地方,以前保存它或致電LocationProvider.getLastKnownLocation)。
相關問題