调用 getSystemService(Context.LOCATION_SERVICE) 并且未调用 onLocationChanged 时出现 NullpointerException
Posted
技术标签:
【中文标题】调用 getSystemService(Context.LOCATION_SERVICE) 并且未调用 onLocationChanged 时出现 NullpointerException【英文标题】:NullpointerException when calling getSystemService(Context.LOCATION_SERVICE) and onLocationChanged not called 【发布时间】:2013-01-05 06:16:24 【问题描述】:我正在尝试创建一个更新当前 gps 位置的后台服务。我在LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
行收到NullPointerException
HomeActivity 启动服务
startService(new Intent(getApplicationContext(), ForHire.class));
Service (ForHire) 创建一个 TimerTask 更新
public class ForHire extends Service
...
private Timer getUpdatesNow = new Timer();
private Updates updates = new Updates(getUpdatesNow);
@Override
public void onCreate()
...
getUpdatesNow.schedule(updates, God.KM20TIME);
Log.v("Taxeeta", "Notification created");
private class Updates extends TimerTask implements LocationListener
private Timer getUpdatesNow;
public Updates(Timer newGetUpdatesNow)
super();
getUpdatesNow = newGetUpdatesNow;
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
God.KM20TIME, God.KM20DISTANCE, (LocationListener) this);
public void run()
...
//do some cleanup
@Override
public void onLocationChanged(Location location)
Log.v("Taxeeta", "Location changed");
// do a update of the current location.
第一个问题是我得到了 NullPointerException。
第二个问题是,如果我注释掉 LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, God.KM20TIME, God.KM20DISTANCE, (LocationListener) this);
这两行,则永远不会调用 onLocationChanged。
我的清单
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application ...>
...
<uses-library android:name="com.google.android.maps" />
</application>
我在这里错过了什么?
编辑:Values of KM20TIME = 5000
(5 秒)和KM20DISTANCE = 1
(1 米)。除了下面的修复之外,我走出家门,启用 GPS,然后从阳台的一个角落走到另一个角落。我注意到我每 5 秒从一个角落走到另一个角落时,我的 gps (LSB) 会发生变化。
【问题讨论】:
【参考方案1】:您最早可以调用new Updates(getUpdatesNow);
是在您的Service 的onCreate()
方法中。原因很简单,getSystemService()
需要在输入onCreate()
之前不存在的有效上下文。
试试:
private Timer getUpdatesNow;
private Updates updates;
@Override
public void onCreate()
...
getUpdatesNow = new Timer();
updates = new Updates(getUpdatesNow);
【讨论】:
谢谢,修复了空指针异常。但是 onLocationChanged 仍然没有被调用。 嗯,我不知道God.KM20TIME
和 God.KM20DISTANCE
是什么,但第一次修复应该没关系。但是您是否处于可以获得 GPS 信号的位置? (我必须走到外面......)
KM20TIME 是 5 秒,KM20DISTANCE = 1 这样我就可以测试它了。
当您注释掉requestLocationUpdates()
时,您从未获得位置,因为这是启动更新的行。但是我看不出你现在不应该获取更新的明显原因......其他 GPS 应用程序在你所在的地方也能工作吗,比如谷歌地图?
工作中,谢谢。你说的一切都是正确的。为你点赞。【参考方案2】:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
Toast.makeText(getBaseContext(),
"Last Location is found..",
Toast.LENGTH_LONG).show();
else
Toast.makeText(getBaseContext(),
"Last Location is not found..",
Toast.LENGTH_LONG).show();
LocationListener locationListener = new LocationListener()
public void onStatusChanged(String provider,
int status, Bundle extras)
public void onProviderEnabled(String provider)
Toast.makeText(getApplicationContext(),
"Gps Enabled", Toast.LENGTH_SHORT).show();
public void onProviderDisabled(String provider)
Toast.makeText(getApplicationContext(),
"Gps Disabled", Toast.LENGTH_SHORT).show();
@Override
public void onLocationChanged(Location location)
Log.i("geolocation",
"lat is : " + location.getLatitude()
+ " and " + location.getLongitude()
+ "");
Toast.makeText(getBaseContext(),
"Location is Changed..", Toast.LENGTH_LONG)
.show();
;
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
【讨论】:
【参考方案3】:@Override
public void onCreate()
new Updates(getUpdatesNow);
...
getUpdatesNow.schedule(updates, God.KM20TIME);
Log.v("Taxeeta", "Notification created");
【讨论】:
以上是关于调用 getSystemService(Context.LOCATION_SERVICE) 并且未调用 onLocationChanged 时出现 NullpointerException的主要内容,如果未能解决你的问题,请参考以下文章