使用带有LBS JSR-179和JSR-179 Extended的Location API时,城市名称的空指针异常

时间:2012-10-04 23:49:12

标签: java blackberry blackberry-simulator blackberry-jde

我是BlackBerry OS开发的新手,我的代码试图在传递LocationProvider参数后使用Criteria时获取城市名称。 我按照RIM本身link跟随我试过“JSR-179”和“JSR-179 Extension” 我的代码如下:

“使用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;
    }

我在那里遗漏了什么或者有什么问题我现在已经在这个问题上抓了好几个小时了。 我也尝试从此代码段中捕获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

谢谢,

1 个答案:

答案 0 :(得分:0)

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

无论您何时尝试在非事件线程上显示Dialog,都应该需要事件锁定。 您可以使用上述方法进行事件锁定 阅读article了解更多信息。