1

我正在尝试将 jsr-179 APi 实施到诺基亚 Symbian 手机中,以便通过 J2me 使用 setLocationListener 进行定期位置更新。在模拟器中它工作正常。当我在设备 nokia 5230 上安装 Midlet 时,它出现 NullPointerException 并且应用程序自动终止。可能的原因是什么?

下面是我的课,我在 netbeans 的表单上为这个类实例化对象

class MovementTracker implements LocationListener {

        LocationProvider provider;
        Location lastValidLocation;
        UpdateHandler handler;
        boolean done;

    public MovementTracker() throws LocationException
    {

    done = false;
    handler = new UpdateHandler();
    new Thread(handler).start();

        //Defining Criteria for Location Provider
       /*
        Criteria cr = new Criteria();
        cr.setHorizontalAccuracy(500);
       */


        //you can place cr inside getInstance
    provider = LocationProvider.getInstance(null);

        //listener,interval,timeout,int maxAge
        //Passing -1 selects default interval
       // provider.setLocationListener(MovementTracker.this, -1, -1, -1);
         provider.setLocationListener(MovementTracker.this, -1, 30000, 30000);

    }

    public void locationUpdated(LocationProvider provider, Location location)
    {
    handler.handleUpdate(location);
        batteryLevel = System.getProperty("com.nokia.mid.batterylevel");
        sn = System.getProperty("com.nokia.mid.networksignal");
        localTime =  System.currentTimeMillis();

        Send_Location();
    }

    public void providerStateChanged(LocationProvider provider, int newState)
    {
    }

    class UpdateHandler implements Runnable
    {
    private Location updatedLocation = null;

    // The run method performs the actual processing of the location

    public void run()
        {
        Location locationToBeHandled = null;
        while (!done)
            {
        synchronized(this)
                {
            if (updatedLocation == null)
                    {
            try
                        {
                wait();
            }
                        catch (Exception e)
                        {
                // Handle interruption
            }
            }
            locationToBeHandled = updatedLocation;
            updatedLocation = null;
        }

        // The benefit of the MessageListener is here.
        // This thread could via similar triggers be
        // handling other kind of events as well in
        // addition to just receiving the location updates.
        if (locationToBeHandled != null)
            processUpdate(locationToBeHandled);
        }
                try
                {
                    Thread.sleep(10000); //Sleeps for 10 sec & then sends the data
                }
                catch (InterruptedException ex)
                {
                }


    }

    public synchronized void handleUpdate(Location update)
        {
        updatedLocation = update;
        notify();
    }

    private void processUpdate(Location update)
        {
        latitude = update.getQualifiedCoordinates().getLatitude();
            longitude = update.getQualifiedCoordinates().getLongitude();
            altitude = update.getQualifiedCoordinates().getAltitude();

    }
    }
} 
4

1 回答 1

1
public MovementTracker() throws LocationException

...
我还没有编写任何处理 LocationException 的代码。

没有代码是非常危险的做法,只需在网上搜索“java 燕子异常”之类的东西。

由于实现细节,诺基亚很有可能在模拟器没有抛出的地方抛出 LocationException。由于您不处理异常,这可能确实会使您在诺基亚的 midlet 崩溃 - 您不会知道原因,因为您再次没有编写任何代码来处理它。

我怎样才能捕捉到那个异常?

您可以做的最简单的事情是在用户阅读并关闭警报后显示带有异常消息的警报并退出 midlet

于 2012-01-17T09:10:11.713 回答