1
    @Override
protected void onCreate(Bundle savedInstanceState) {
    statusCheck();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getLocationPermission();

public void statusCheck() {
    final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Intent intent = new Intent(MainActivity.this, NewActivity.class);
        buildAlertMessageNoGps();

    }
}

private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                    dialog.cancel();
                }
            });
    final AlertDialog alert = builder.create();
    alert.show();
}

    private void getLocationPermission(){
    Log.d(TAG, "getLocationPermission: getting location permissions");
    String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION};

    if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
            FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
                COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            mLocationPermissionsGranted = true;
            initMap();
        }else{
            ActivityCompat.requestPermissions(this,
                    permissions,
                    LOCATION_PERMISSION_REQUEST_CODE);
        }
    }else{
        ActivityCompat.requestPermissions(this,
                permissions,
                LOCATION_PERMISSION_REQUEST_CODE);
    }
}

if the location settings is off in android settings, the alert dialog will pop up but very soon after that the app will crash and users cannnot choose ok to enable settings. How do I make it so that the app will not crash immediately

crash log 2022-03-12 18:33:22.581 16405-16405/com.example.wheelchairaccessible E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.wheelchairaccessible, PID: 16405 java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference at com.example.wheelchairaccessible.MainActivity$3.onComplete(MainActivity.java:2129) at com.google.android.gms.tasks.zzj.run(com.google.android.gms:play-services-tasks@@17.2.0:4) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8633) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Hou Feng
  • 21
  • 2
  • Add your crash log as well – WhiteSpidy. Mar 12 '22 at 08:42
  • Also if permission is disabled you are already showing an alert in `statusCheck()` so what are you performing in `getLocationPermission ()` are you again trying to ask a permission in that ? – WhiteSpidy. Mar 12 '22 at 08:44
  • Hey, thanks. I have just updated it. – Hou Feng Mar 12 '22 at 10:34
  • My get locationpermission works but if the user has location settings off, it will not work and the app will immediately crash. This is why I added statuscheck. – Hou Feng Mar 12 '22 at 10:35
  • Does this answer your question? [Error invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference](https://stackoverflow.com/questions/32290045/error-invoke-virtual-method-double-android-location-location-getlatitude-on) – WhiteSpidy. Mar 12 '22 at 11:00

0 Answers0