SharedPreferences NullPointerException继续显示

时间:2015-06-11 07:12:46

标签: java android parse-platform nullpointerexception sharedpreferences

尝试了多件事,我无法理解为什么我得到了nullPointer。数组myStrings中有数据,但它的表现却没有。有谁发现了这个问题? 这是整个代码:

public class GPSTracker extends Service implements LocationListener {


private final Context context;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5;
private static final long MIN_TIME_BW_UPDATES = 5000;
private ListViewAdapter urgentTodosAdapter;
protected LocationManager locationManager;

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

public Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(!isGPSEnabled && !isNetworkEnabled) {

        } else {
            this.canGetLocation = true;

            if (isNetworkEnabled) {

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    if (location != null) {

                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }

            }

            if(isGPSEnabled) {
                if(location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if(locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if(location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();

                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}


public void stopUsingGPS() {
    if(locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude() {
    if(location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude() {
    if(location != null) {
        longitude = location.getLongitude();
    }

    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

    alertDialog.setTitle("GPS is settings");

    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    });

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {

    this.location = location;
    getLatitude();
    getLongitude();

    ParseUser user2 = ParseUser.getCurrentUser();
    ParseGeoPoint geoPoint = new ParseGeoPoint(getLatitude(), getLongitude());
    user2.put("lat_long", geoPoint);
    user2.saveInBackground();

    Log.d("Coordinates", getLatitude() + " " + getLongitude());

    String username = user2.getUsername();
    ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
    query.whereWithinKilometers("lat_long", geoPoint, 0.03);
    query.whereNotEqualTo("username", username);

    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> listUsers, ParseException e) {

            for (ParseObject object : listUsers){

                String ids = object.getString("fullname");
                Log.d("These people are here:"," "+ids);

                SharedPreferences sharedPrefs = getSharedPreferences("com.parseapp.eseen.eseen", 0);
                Boolean onOFF = sharedPrefs.getBoolean("ToggleOnOff",true);
                SharedPreferences settings = getSharedPreferences("NotificationIDs", 0);
                SharedPreferences.Editor editor = settings.edit();
                Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
                ParseInstallation installation = ParseInstallation.getCurrentInstallation();




                if (onOFF) {
                    int a = 0;
                    for (String checking : myStrings) {

                        if (object.getObjectId().equals(checking)) {
                            a = 1;
                        }
                    }
                    if (a == 1) {
                        Log.d("This user: ", object.getString("fullname"));

                    } else {

                        ParseUser user = ParseUser.getCurrentUser();
                        ParseQuery query = ParseInstallation.getQuery();
                        query.whereEqualTo("user", user.getObjectId());
                        installation.saveInBackground();


                        ParsePush androidPush = new ParsePush();
                        androidPush.setMessage(object.getString("fullname") + " is near you!");
                        androidPush.setQuery(query);
                        androidPush.sendInBackground();

                    }

                }

                    myStrings.add(object.getObjectId());
                    editor.putStringSet("myStrings", myStrings);
                    editor.apply();

            }

        }
    });







}

快速解释,它会检查具有特定条件的用户,制作一个列表,然后检查用户是否是新用户,如果确实如此,则会向用户发送通知并将其保存在“历史记录列表”中,如果用户再次出现他将不会收到通知。

这是logcat:

06-11 09:03:38.211  29052-29052/com.parseapp.eseen.eseen E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
        at com.parseapp.eseen.eseen.GPSTracker$3.done(GPSTracker.java:200)
        at com.parseapp.eseen.eseen.GPSTracker$3.done(GPSTracker.java:192)
        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5511)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
        at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

更改您的共享偏好设置代码,如下所示....

SharedPreferences sharedPrefs = getSharedPreferences("sharedprefsname", 0);
sharedprefs.getBoolean("ToggleOnOff",true);

答案 1 :(得分:0)

确保在调用活动getSharedPreferences()后调用onCreate()

答案 2 :(得分:0)

嗯,是的,我唯一需要改变的是背景。

SharedPreferences sharedPrefs = context.getSharedPreferences("com.parseapp.eseen.eseen", Context.MODE_PRIVATE);
                Boolean onOFF = sharedPrefs.getBoolean("ToggleOnOff", true);
                SharedPreferences settings = context.getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();
                Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
                ParseInstallation installation = ParseInstallation.getCurrentInstallation();

现在工作正常,谢谢大家。