App crashes onstart with a null pointer exception. this happens when location permission is already granted. However doesnt occur when the location permission is not granted and only given after the app starts.
public void requestLocationUpdates() {
Log.i(TAG, "Requesting location updates");
Utils.setRequestingLocationUpdates(this, true);
startService(new Intent(getApplicationContext(),
LocationUpdatesService.class));
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
mLocationCallback, Looper.myLooper());
} catch (SecurityException unlikely) {
Utils.setRequestingLocationUpdates(this, false);
Log.e(TAG, "Lost location permission. Could not request updates. " +
unlikely);
}
}
Below is the error.
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.projectu/com.example.projectu.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.example.projectu.LocationUpdatesService.requestLocationUpdates()' on a
null object reference
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
com.example.projectu.LocationUpdatesService.requestLocationUpdates()' on a
null object reference
at com.example.projectu.MainActivity.onStart(MainActivity.java:265)
at
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
at android.app.Activity.performStart(Activity.java:7029)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)