【发布时间】:2012-01-17 17:10:11
【问题描述】:
我正在尝试将 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();
}
}
}
【问题讨论】:
-
你能提供你有问题的代码吗?最好采用SSCCE 形式
-
我将代码 sn-p 从您的“答案” (stackoverflow.com/a/8877693/839601) 复制到问题中。建议删除“答案”
-
好的!你能告诉我,我在哪里做错了吗?
-
不是完全错误,但可能有风险的是 MovementTracker 构造函数抛出 LocationException。你如何处理这个异常?
-
我还没有编写任何处理LocationException的代码。我怎样才能捕捉到那个异常?有代码吗?
标签: exception java-me location jsr179