Android GPS 错误
当我尝试通过 GPS 或网络获取当前位置时,我的应用程序出现 GPS 错误
private static LocationManager lm;
private static LocationListener locationListener;
private static Location posicion;
private double latitud;
private double longitud;
posicion = ObtenPosicion();
latitud = posicion.getLatitude();
longitud= posicion.getLongitude();
//Me centro en el mapa
final GeoPoint yo = new GeoPoint((int)(latitud),(int)(longitud));
public Location ObtenPosicion(){
if (lm == null)
{
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocListener();
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
LOCATION_UPDATE_TIME,
LOCATION_UPDATE_DISTANCE,
locationListener);
posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
LOCATION_UPDATE_TIME,
LOCATION_UPDATE_DISTANCE,
locationListener);
posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
return posicion;
}
private class LocListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc) {
posicion.set(loc);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
}
public static Location getCurrentLocation ()
{
return posicion;
}
public void stopLocationListening ()
{
if (lm != null)
lm.removeUpdates(locationListener);
}
该错误是一行中的空指针异常:
12-06 17:04:25.418: E/AndroidRuntime(4744): java.lang.NullPointerException
12-06 17:04:25.418: E/AndroidRuntime(4744): at com.rbrlnx.lugares.editarLugar$LocListener.onLocationChanged(editarLugar.java:215)
我尝试使用不同的代码,但这是不可能的..
I have a GPS error in my app when I am trying to get my current location, by gps or by network
private static LocationManager lm;
private static LocationListener locationListener;
private static Location posicion;
private double latitud;
private double longitud;
posicion = ObtenPosicion();
latitud = posicion.getLatitude();
longitud= posicion.getLongitude();
//Me centro en el mapa
final GeoPoint yo = new GeoPoint((int)(latitud),(int)(longitud));
public Location ObtenPosicion(){
if (lm == null)
{
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocListener();
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
LOCATION_UPDATE_TIME,
LOCATION_UPDATE_DISTANCE,
locationListener);
posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
LOCATION_UPDATE_TIME,
LOCATION_UPDATE_DISTANCE,
locationListener);
posicion = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
return posicion;
}
private class LocListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc) {
posicion.set(loc);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
}
public static Location getCurrentLocation ()
{
return posicion;
}
public void stopLocationListening ()
{
if (lm != null)
lm.removeUpdates(locationListener);
}
The error is a nullpointerexception in a line :
12-06 17:04:25.418: E/AndroidRuntime(4744): java.lang.NullPointerException
12-06 17:04:25.418: E/AndroidRuntime(4744): at com.rbrlnx.lugares.editarLugar$LocListener.onLocationChanged(editarLugar.java:215)
I tried with a different codes but its me impossible..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题在于您没有分配“posicio”对象。您只需将其声明为静态即可。因此,当您在 onLocationChanged 回调中从系统获取回调时,您尝试使用 null 对象。
在 onCreate 方法中,您需要使用以下方法分配和初始化位置:
Your issue is with the fact that you did not allocate a 'posicio' object. You simply declared it as static. So, when you get the callback from the system in the onLocationChanged callback you try to use a null object.
In your onCreate method, you need to allocate and initialize the posicion with:
posicion 变量在这里为 null.... 初始化它尝试两个中的任何一个/两者
在
onCreate()
方法中,使用默认值初始化 posicion。和/或在必要时使用 try/catch 在适当的位置调用ObtenPosicion()
The posicion variable is null here.... Initialize it try any/both of the two
In
onCreate()
method, initialize posicion with a default value. And/or than callObtenPosicion()
at appropriate place with try/catch if necessary