我想要做的是通过单击带有两个条件的按钮字段打开地图,用户必须指定位置,然后必须将图像添加到该位置,否则必须将图像添加到用户当前位置。
我遇到的问题是将两个条件都添加到线程/新线程甚至 FieldChangeListener 中的 if 语句中。
我不断收到的错误是:
位置错误:javax.microedition.location.LocationException:getLocation() 方法无法从事件线程 [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);
请更详细地告诉我如何做到这一点,并举个例子;我无法理解“mMapField”是如何自定义 MapField 而“mapView”是 Mapview 类(请参阅我上面的代码片段)。