在J2me上实现Location API时出现NullPointerException错误

时间:2012-01-13 09:23:38

标签: exception java-me location jsr179

我正在尝试将jsr-179 APi应用到诺基亚Symbian手机中,以便通过J2me使用setLocationListener定期更新位置。在模拟器中,它工作正常。当我在诺基亚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();

    }
    }
} 

1 个答案:

答案 0 :(得分:1)

public MovementTracker() throws LocationException
     

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

没有代码是非常危险的做法,只需在网上搜索“java swallow例外”等内容。

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

  

如何捕获该异常?

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