【发布时间】:2012-10-04 23:49:11
【问题描述】:
我对 BlackBerry OS 开发有点陌生,我的代码试图在传递 Criteria 参数后使用 LocationProvider 获取城市名称。
我根据 RIM 本身的 link 关注我尝试了“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-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;
}
我是否遗漏了什么,或者有什么问题我已经在这个问题上苦苦琢磨了好几个小时了。
我也尝试从这段代码 sn-p 中捕获 Null Pointer Exception,如果从“static void main”中捕获,则 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
谢谢,
【问题讨论】:
-
Dialog.alert()不是记录异常的好方法。对于您的情况,它会抛出“由非事件线程调用的 pushModalScreen”。使用Logger类或设备内存/sd 卡上的文本文件来记录您想要的信息。关于您的代码,似乎 cellLocation.CurrentLocation() 返回空值。在您的情况下,这就是 NPE 的原因。确保在实际设备上运行此代码时您有可用的 GPS 卫星,并使用 Logger 类记录您需要执行的每个suspicious步骤。要使用键盘查看设备上的日志,请点击 CTRL,按住它并点击键盘上的LGLG。
标签: java blackberry blackberry-simulator blackberry-jde