位置错误:无法从事件线程调用getLocation()方法

时间:2012-10-04 10:37:50

标签: blackberry error-handling gps location

我想要做的是打开地图,点击带有两个标准的按钮区域,用户必须指定位置,然后必须将图像添加到该位置,否则必须将图像添加到用户当前位置。

我遇到的问题是将两个标准添加到线程/新线程中的if语句中,甚至是在FieldChangeListener中。

我不断得到的错误是:

  

位置错误:javax.microedition.location.LocationException:   getLocation()方法不能是来自事件线程[0.0]

的cal [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);

请详细告诉我如何操作,请举个例子;我无法理解“mMapField”是一个自定义的MapField,“mapView”是一个Mapview类(请参阅上面的代码片段)。

2 个答案:

答案 0 :(得分:2)

获取一个位置是一项耗时的任务,即使具有良好的卫星可见性,它也可能需要多达1分钟,尽管较新的浆果已经大大改善了首次定位时间(TTFF)。

不应该在事件线程中执行耗时的任务,比如打开连接或获取修复,因为这个线程必须响应用户事件,如果你吃了它,那么GUI就会被淘汰。在fieldChanged内运行的所有内容都在事件线程中运行。因此,RIM在其新的BlackBerryLocationProvider中实现线程检测并抛出异常是件好事,现在您已经意识到设计不良并且可以采取纠正措施。

您可以通过多种方式异步修复修补程序:

  1. 使用LocationListener
  2. 产生一个新线程。
  3. 在您需要之前(或定期)快速获得修复,然后按下按钮时可以快速获得修复(从以前保存过的地方检索它或调用LocationProvider.getLastKnownLocation )。

答案 1 :(得分:0)

您应该使用Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments(mMapField));代替add(mMapField);

相关问题