I created an android app and the app work perfectly in android below 12. I get crash when app run in android 12 and the error is about the below line. provider variable is null and I don't know why? I've read about location and background location in android 11+ and I do them but I have this error.
public static boolean isGPSEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
String provider = Settings.Secure.getString(KITILApplication.getappContext().getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (provider.contains("gps"))
return true;
if (locationManager != null)
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return true;
}
return false;
}
Error is:
Process: com., PID: 14005
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
at com.BaseActivity.isGPSEnabled(BaseActivity.java:78)
at com.recordLogOnDB(HelperMethods.java:390)
at com.LoginDialogActivity$3.onResponse(LoginDialogActivity.java:329)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)