2012-01-13 32 views
1

我正在尝试将jsr-179 APi实现到诺基亚Symbian手机中,以通过J2me使用setLocationListener进行定期位置更新。在模拟器中它工作正常。当我在设备nokia 5230上安装了Midlet时,它被给予NullPointerException,并且应用程序自动终止。可能的原因是什么?在J2ME上实现位置API的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(); 

    } 
    } 
} 
+0

你能提供的代码你有问题?最好在[SSCCE](http://www.sscce.org/)表格​​ – gnat 2012-01-13 10:53:12

+1

我从你的“答案”(http://stackoverflow.com/a/8877693/839601)复制代码片段到问题中。建议删除“答案” – gnat 2012-01-16 10:24:38

+0

好的!你能告诉我,我在哪里做错了? – 2012-01-16 10:54:07

回答

1
public MovementTracker() throws LocationException 

实例化对象该类...
我没有写处理LocationException任何代码。

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

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

如何捕获该异常?

你可以做最简单的事情是用户读取后显示有异常消息警报并退出MIDlet并驳回警报