0

尝试了多种事情我只是无法得到它为什么我得到nullPointer。数组myStrings中有数据,但它的行为不像。任何人发现问题? 这里是整个代码:SharedPreferences NullPointerException保持显示

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) 
+0

哪一行是153? – vefthym

+0

'.edit()'你在'sharedPreference'的声明中忘记了。看看[这](http://stackoverflow.com/questions/23024831/android-shared-preferences-example)。 –

回答

0

变化如下共享偏好代码....

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

试过了,它没有工作。 – Jawee

+0

是否在您的共享首选项文件中更改了它? –

+0

是的,我得到完全相同的东西。等待编辑整个代码,并把完整的一个在这里。 – Jawee

0

确保您onCreate()一直呼吁的活动后调用getSharedPreferences()

+0

如果我试图在Android服务中调用它,该怎么办? – Jawee

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(); 

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

相关问题