2012-01-13 36 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並駁回警報