startActivity throws error when trying to launch Settings.ACTION_LOCATION_SOURCE_SETTINGS

Sparker0i

PS: I've tried all sorts of solutions of the given type but none of them worked for me.

I have a GPSTracker class configured like this:

public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;
    ...
    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }
}

This getLocation() will be invoked immediately. Now the issue I am having is in a function called showSettingsAlert() implemented in the same class like this:

public void showSettingsAlert(){
    new MaterialDialog.Builder(mContext)
            .content("GPS Needs to be enabled to view Weather Data of your Location")
            .title("Enable GPS")
            .positiveText("ENABLE")
            .onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    if (intent != null)
                        startActivity(intent);
                    else
                        Log.i("Null" , "Intent");
                }
            })
            .negativeText("CANCEL")
            .onNegative(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    dialog.dismiss();
                }
            })
            .show();
}

When this dialog box is displayed, and when I click on ENABLE, I get the following dump:

04-15 16:59:12.288 3982-3982/com.a5corp.weather E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: com.a5corp.weather, PID: 3982
                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.startActivity(android.content.Intent)' on a null object reference
                                                                  at android.content.ContextWrapper.startActivity(ContextWrapper.java:356)
                                                                  at com.a5corp.weather.permissions.GPSTracker$2.onClick(GPSTracker.java:166)
                                                                  at com.afollestad.materialdialogs.MaterialDialog.onClick(MaterialDialog.java:361)
                                                                  at android.view.View.performClick(View.java:5637)
                                                                  at android.view.View$PerformClick.run(View.java:22429)
                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6169)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
jessica
private GPSTracker myGpsTracker;

 protected void onCreate(Bundle savedInstanceState) {
        myGpsTracker = new GPSTracker(this);
        if (myGpsTracker.canGetLocation())
        {
            double longitude = myGpsTracker.latitude;
            double latitude =  myGpsTracker.longitude;
        }
        else
        {
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            showSettingsAlert();
        }
 }

    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     */
    public void showSettingsAlert() 
    {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(SplashScreen.this);

        // Setting Dialog Title
        alertDialog.setTitle("GPS Settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not active. Do you want to open?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, CommonUtils.REQUEST_CODE_ACTION_LOCATION_SOURCE_SETTINGS);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                setLocationValues();

            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

      @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonUtils.REQUEST_CODE_ACTION_LOCATION_SOURCE_SETTINGS) {
            setLocationValues();
        }
    }

     private void setLocationValues() {
        myGpsTracker.getLocation();
        if (myGpsTracker.canGetLocation()) {
            double longitude = myGpsTracker.latitude;
            double latitude = myGpsTracker.longitude;
        } 
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive