Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Android在Java中初始化对象上的NullPointerException_Java_Android_Parsing_Parse Platform - Fatal编程技术网

使用Android在Java中初始化对象上的NullPointerException

使用Android在Java中初始化对象上的NullPointerException,java,android,parsing,parse-platform,Java,Android,Parsing,Parse Platform,对于Android开发来说,这是一个相当新的东西,在我的Udemy教程之后。获取对象上的nullpointerexception,即使它将值打印到日志中。看起来是driverLocation对象出错了,但我已经尝试在声明和onCreate中初始化它。我正在使用parse.com保存数据 下面是发生此错误的java文件或活动中的代码。项目中还有4个其他java文件,请告诉我是否应该发布更多。在代码下面发布日志消息。我已经在这上面呆了一段时间了,很明显,我忘记了一些事情 public class Y

对于Android开发来说,这是一个相当新的东西,在我的Udemy教程之后。获取对象上的nullpointerexception,即使它将值打印到日志中。看起来是driverLocation对象出错了,但我已经尝试在声明和onCreate中初始化它。我正在使用parse.com保存数据

下面是发生此错误的java文件或活动中的代码。项目中还有4个其他java文件,请告诉我是否应该发布更多。在代码下面发布日志消息。我已经在这上面呆了一段时间了,很明显,我忘记了一些事情

public class YourLocation extends FragmentActivity implements OnMapReadyCallback, LocationListener {

Location location;
private GoogleMap mMap;
LocationManager locationManager;
String provider;
TextView infoTextView;
Button requestDriverButton;
Boolean requestActive;
String driverUsername = "";
ParseGeoPoint driverLocation = new ParseGeoPoint(10,10);
Handler handler = new Handler();
public void requestDriver(View view) {

    if (requestActive == false) {

        Toast.makeText(getApplicationContext(), "Driver Requested", Toast.LENGTH_SHORT).show();

        final ParseObject request = new ParseObject("Request");
        request.put("requesterUsername", ParseUser.getCurrentUser().getUsername());

        ParseACL parseACL = new ParseACL();
        parseACL.setPublicWriteAccess(true);
        parseACL.setPublicReadAccess(true);
        request.setACL(parseACL);

        request.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e == null) {
                    infoTextView.setText("Waiting for driver...");
                    requestDriverButton.setText("Cancel ride");
                    requestActive = true;
                    updateLocation(location);
                }
            }
        });

    } else {
        infoTextView.setText("Ride cancelled");
        requestDriverButton.setText("Request Driver");
        requestActive = false;

        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Request");
        query.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    if (objects.size() > 0) {
                        for (ParseObject object : objects) {
                            object.deleteInBackground(new DeleteCallback() {
                                @Override
                                public void done(ParseException e) {
                                    Toast.makeText(getApplicationContext(), "Ride Cancelled", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }
            }
        });
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your_location);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    infoTextView = (TextView) findViewById(R.id.infoTextView);
    requestDriverButton = (Button) findViewById(R.id.requestDriver);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); //Context.LOCATION_SERVICE??
    provider = locationManager.getBestProvider(new Criteria(), false);

    locationManager.requestLocationUpdates(provider, 400, 1, this);

    requestActive = false;
}

@Override
protected void onResume() {
    super.onResume();

    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

@Override
protected void onPause() {
    super.onPause();

    locationManager.removeUpdates(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    location = locationManager.getLastKnownLocation(provider);

    if (location != null) {

        updateLocation(location);
    }
}

public void updateLocation(final Location location) {

    mMap.clear();

    if (requestActive == false) {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Request");
        query.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    if (objects.size() > 0) {
                        for (ParseObject object : objects) {
                            requestActive = true;
                            infoTextView.setText("Waiting for driver...");
                            requestDriverButton.setText("Cancel ride");

                            if (object.get("driverUsername") != null) {

                                driverUsername = object.getString("driverUsername");
                                infoTextView.setText("A driver is on the way");
                                requestDriverButton.setVisibility(View.INVISIBLE);

                            }
                        }
                    }
                }
            }
        });
    }

    if (driverUsername.equals("")) {

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10));

        mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Your Location"));
    }

    if (requestActive == true) {

        if (!driverUsername.equals("")) {

            ParseQuery<ParseUser> query = ParseUser.getQuery();
            query.whereEqualTo("username", driverUsername);
            query.findInBackground(new FindCallback<ParseUser>() {
                @Override
                public void done(List<ParseUser> objects, ParseException e) {
                    if (e == null) {
                        if (objects.size() > 0) {
                            for (ParseUser driver : objects) {
                                driverLocation = driver.getParseGeoPoint("location");
                            }
                        }
                    }
                }
            });

            Log.i("test", driverLocation.toString());

            Log.i("latitude", String.valueOf(driverLocation.getLatitude()));
            Log.i("longitude", String.valueOf(driverLocation.getLongitude()));

            if (driverLocation.getLatitude() != 0.0 && driverLocation.getLongitude() != 0.0) {
                Log.i("app", driverLocation.toString());

                Double distanceInKM = driverLocation.distanceInKilometersTo(new ParseGeoPoint(location.getLatitude(), location.getLongitude()));

                NumberFormat format = new DecimalFormat("#0.00");

                infoTextView.setText("Your driver is " + format.format(distanceInKM) + " km away");

                LatLngBounds.Builder builder = new LatLngBounds.Builder();

                ArrayList<Marker> markers = new ArrayList<>();

                markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(driverLocation.getLatitude(), driverLocation.getLongitude())).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)).title("Driver Location")));

                markers.add(mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Your Location")));

                for (Marker marker : markers) {
                    builder.include(marker.getPosition());
                }

                LatLngBounds bounds = builder.build();

                int padding = 300;
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);

                mMap.animateCamera(cameraUpdate);
            }

        }

        final ParseGeoPoint userLocation = new ParseGeoPoint(location.getLatitude(), location.getLongitude());

        ParseQuery<ParseObject> query1 = new ParseQuery<ParseObject>("Request");
        query1.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
        query1.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    for (ParseObject object : objects) {
                        object.put("requesterLocation", userLocation);
                        object.saveInBackground();
                    }
                }
            }
        });

    }

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            updateLocation(location);
        }
    }, 2000);

}

@Override
public void onLocationChanged(Location location) {

    mMap.clear();

    updateLocation(location);

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}

我看不出您在onCreate中在何处定义了driverLocation,因此在updateLocation方法中调用onCreate后,如果e==null或objects.size>0返回false,则driverLocation将不会被定义,它将为null,从而导致driverLocation.toString抛出NullPointerException

我建议将driverLocation声明在您现在拥有的类的顶部:

ParseGeoPoint driverLocation;
然后在onCreate方法中定义它,这样它将始终被定义为NOTNULL

driverLocation = new ParseGeoPoint(10,10);

看来Iv'e在语法分析上搞砸了。对于那些想知道的人,我将driverLocation的值设置为driver.getParseGeoPointlocation,该值为null,所以它就存在了。在Parse中更新了一些东西,它就像一个符咒一样工作。谢谢大家的努力

可能的副本尝试了该操作,但仍然得到相同的异常。
driverLocation = new ParseGeoPoint(10,10);