Tuesday, 20 August 2013

Android GPS null pointer exeception

Android GPS null pointer exeception

Basically, I don't know if this is Eclipse's problem or my phone's GPS or
the my app's code.
The problem: When I try restart my phone and run my app, it crashes on
Runtime. The error is NullPointer (no line number is provided). But if I
remove the two lines from onCreate() which checks for GPS, and run the app
again, it works. Then if I re-add those lines again, the app works. So
basically, the code is fine but it crashes the App if it's being run for
the first time after phone has been restarted. Here is some relevant code:
onCreate()
...
// Enable Google Gps
map.setMyLocationEnabled(true);
// ERROR: if I remove these lines, run the app it works, then after the
app successfully runs for the first time, I can re-add these lines and it
will work normally.
// As it's a NullPointer with the GPS, my guess it, the system reports the
GPS is active, but it's not being run properly and returns a null
location?
// My Location - if Gps active, get my location and move camera to it
LatLng ll = new LocTools(this).getMyLocation();
if (ll != null) {
LocTools.goToLocation(map,ll,3,true);
}
...
LocTools.getMyLocations()
public LatLng getMyLocation() {
final LocationManager manager = (LocationManager)
mContext.getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
final AlertDialog.Builder builder = new
AlertDialog.Builder(mContext);
builder.setTitle("GPS");
builder.setIcon(R.drawable.gps);
builder.setMessage("Please enable GPS satellites then try again")
.setCancelable(false)
.setPositiveButton("GPS settings", new
DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int
id) {
mContext.startActivity(new
Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int
id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
return null;
} else {
LocationManager lm = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double lat = l.getLatitude();
double lng = l.getLongitude();
return new LatLng(lat,lng);
}
}
LocTools.goToLocation
/** goToLocation * @param location Address * @param zoom Float * @param b
Boolean - Animate; */ public static void goToLocation(GoogleMap map,
LatLng ll, float zoom, boolean b) { if (b == true) {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(ll, zoom)); } else {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, zoom)); } }
Error
08-20 11:21:49.538: E/AndroidRuntime(9460): FATAL EXCEPTION: main
08-20 11:21:49.538: E/AndroidRuntime(9460): java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.mymaps/com.mymaps.Home}:
java.lang.NullPointerException
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.app.ActivityThread.access$600(ActivityThread.java:140)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.os.Looper.loop(Looper.java:137)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.app.ActivityThread.main(ActivityThread.java:4898)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
java.lang.reflect.Method.invokeNative(Native Method)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
java.lang.reflect.Method.invoke(Method.java:511)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
dalvik.system.NativeStart.main(Native Method)
08-20 11:21:49.538: E/AndroidRuntime(9460): Caused by:
java.lang.NullPointerException
08-20 11:21:49.538: E/AndroidRuntime(9460): at
com.mymaps.LocTools.getMyLocation(LocTools.java:53)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
com.mymaps.Home.onCreate(Home.java:35)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.app.Activity.performCreate(Activity.java:5206)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
08-20 11:21:49.538: E/AndroidRuntime(9460): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
08-20 11:21:49.538: E/AndroidRuntime(9460): ... 11 more

No comments:

Post a Comment