GeoPoints will not parse in Android app

2019-09-08 15:30发布

So I have an app that caches geopoints per user and then saves it on Parse.com and then updates my map with last known coordinates of all users. But it crashes and I can't find the reason why. It will work with manually input lats and longitudes, but not caches current lats and long.

This is my method for it;

public class MapActivity extends FragmentActivity implements LocationProvider.LocationCallback {

    public static final String TAG = MapActivity.class.getSimpleName();
    ParseUser currentUser = ParseUser.getCurrentUser();




    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    private LocationProvider mLocationProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
        mLocationProvider = new LocationProvider(this, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
        mLocationProvider.connect();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mLocationProvider.disconnect();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }


    private void setUpMap() {
        //mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

    public void handleNewLocation(Location location) {
        Log.d(TAG, location.toString());

        double currentLatitude = location.getLatitude();
        double currentLongitude = location.getLongitude();
        LatLng latLng = new LatLng(currentLatitude, currentLongitude);
        currentUser.put("lat", currentLatitude);
        currentUser.put("lng", currentLongitude);
        currentUser.saveInBackground();
        ParseGeoPoint point = new ParseGeoPoint(currentLatitude, currentLongitude);
        currentUser.put("location", point);


        ParseQuery<ParseUser> queryParseUser = ParseUser.getQuery();
        queryParseUser.findInBackground(new FindCallback<ParseUser>() {

            @Override
            public void done(List<ParseUser> locationList, ParseException arg1) {
                // TODO Auto-generated method stub

                if (locationList != null && arg1 == null) {
                    for (ParseUser parseObject : locationList) {

                        ParseGeoPoint parseGeoPoint = parseObject.getParseGeoPoint("location");
                        // laver marker

                        MarkerOptions marker = new MarkerOptions().position(
                                new LatLng(parseGeoPoint.getLatitude(), parseGeoPoint.getLongitude())).title(parseObject.getUsername());

                        // BitmapDescriptorFactory.defaultMarker();
                        // adder marker
                        mMap.addMarker(marker);

                    }
                }
                else {
                    Log.d("Lokation", "Fejl i returnering af data: " + arg1.toString());
                }
            }

        });

    }

       }



}

Android reports errors here;

 new LatLng(parseGeoPoint.getLatitude(), parseGeoPoint.getLongitude())).title(parseObject.getUsername());

and here

    queryParseUser.findInBackground(new FindCallback<ParseUser>() {


09-25 01:06:31.092    8413-8413/com.example.nan.spymap E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.nan.spymap, PID: 8413
    java.lang.NullPointerException: Attempt to invoke virtual method 'double com.parse.ParseGeoPoint.getLatitude()' on a null object reference
            at com.example.nan.spymap.MapActivity$1.done(MapActivity.java:126)
            at com.example.nan.spymap.MapActivity$1.done(MapActivity.java:113)
            at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Really stuck on this, any help would be appreciated.:(

0条回答
登录 后发表回答