0
我的應用程序出現問題。當我嘗試獲取GPS位置時,我的應用程序給我一個NullPointerException,我啓動了一個位置管理器和一個位置監聽器。 上下文是MainAplication背景下,以前進口Android GPS NullpointerException
我的代碼:
mLocationManager=(LocationManager)MainActivity.context.getSystemService(Context.LOCATION_SERVICE);
//mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 0, loclistener);
if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)==true){
//skt.MostrarToast(MainActivity.context, "GPS Habilitado...");
Log.i("Manada3", "GPS habilitado");
gpsEn=true;
MainActivity.looperStatus=true;
}else{
//skt.MostrarToast(MainActivity.context, "GPS Deshabilitado...");
//skt.MostrarToast(MainActivity.context, "Favor habilitar GPS...");
Log.i("Manada3", "GPS Deshabilitado");
Toast.makeText(getBaseContext(), "GPS DESHABILITADO",Toast.LENGTH_SHORT).show();
gpsEn=false;
MainActivity.looperStatus=false;
}
if(gpsEn==true){
//locLatitud=mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//Log.i("Manada3", "locLatitud:"+locLatitud);
//mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loclistener);
loclistener=new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("Manada3", "onLocationChanged 1");
if(MainActivity.looperStatus==false){
mLocationManager.removeUpdates(loclistener);
MainActivity.looperStatus=false;
}
// TODO Auto-generated method stub
Log.i("Manada3","GPSLoggerService.onLocationChanged()");
locLatitud2 = location.getLatitude();
locLongitud2 = location.getLongitude();
st_latitud=String.valueOf(locLatitud2);
st_longitud=String.valueOf(locLongitud2);
Log.i("Manada3", "Coordenadas:"+st_latitud+" "+st_longitud);
//soquet.ans2.notify();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Log.i("Manada3","Provider Disabled");
stopSelf();
//Looper.myLooper().quit();
loclistener=null;
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Log.i("Manada3","Provider Enabled");
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
Log.i("Manada3","Status Changed");
}
};
if(mLocationManager==null){
mLocationManager=(LocationManager)MainActivity.context.getSystemService(Context.LOCATION_SERVICE);
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 0, loclistener,Looper.getMainLooper());
locLatitud2 = locLatitud.getLatitude();
locLongitud2 = locLatitud.getLongitude();
Log.i("Manada3", "Coordenadas:"+locLatitud2+" "+locLongitud2);
}else{
Log.i("Manada3","Error 300");
}
}
我logcat的信息:
07-28 13:05:26.678: E/AndroidRuntime(5138): FATAL EXCEPTION: main
07-28 13:05:26.678: E/AndroidRuntime(5138): java.lang.NullPointerException
07-28 13:05:26.678: E/AndroidRuntime(5138): at com.example.manada4.PruebaService.startLoggingService(PruebaService.java:222)
07-28 13:05:26.678: E/AndroidRuntime(5138): at com.example.manada4.PruebaService.access$0(PruebaService.java:148)
07-28 13:05:26.678: E/AndroidRuntime(5138): at com.example.manada4.PruebaService$IncomingHandler.handleMessage(PruebaService.java:86)
07-28 13:05:26.678: E/AndroidRuntime(5138): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 13:05:26.678: E/AndroidRuntime(5138): at android.os.Looper.loop(Looper.java:137)
07-28 13:05:26.678: E/AndroidRuntime(5138): at android.app.ActivityThread.main(ActivityThread.java:4921)
07-28 13:05:26.678: E/AndroidRuntime(5138): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 13:05:26.678: E/AndroidRuntime(5138): at java.lang.reflect.Method.invoke(Method.java:511)
07-28 13:05:26.678: E/AndroidRuntime(5138): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-28 13:05:26.678: E/AndroidRuntime(5138): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-28 13:05:26.678: E/AndroidRuntime(5138): at dalvik.system.NativeStart.main(Native Method)
哪個是'PruebaService.java'中的第222行? –
PruebaService.java是一個包裝GPS連接的服務。 – volivares
是的,可能是這樣,但它似乎在該文件的第222行中崩潰,因此看到這樣會有幫助。 –