简体   繁体   中英

Fatal error caused by a Null Object Reference from CameraUpdateFactory

I keep getting this Fatal Error in my app, caused from a null object reference.

06-06 14:10:43.180 12002-12002/? D/BarreChat: 29.356811:-57.3686681:lat/lng: 
(29.356811,-57.3686681):com.google.android.gms.maps.CameraUpdate@a16f2f9
06-06 14:10:43.181 12002-12002/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.christopher.barrechat, PID: 12002
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
    at com.example.christopher.barrechat.BarreChat$1.onComplete(BarreChat.java:276)
    at com.google.android.gms.tasks.zzj.run(Unknown Source:23)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:180)
    at android.app.ActivityThread.main(ActivityThread.java:6950)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)
06-06 14:10:43.182 2867-15345/? E/ActivityManager: App crashed! Process: com.example.christopher.barrechat

This is the code that gives the error message. You can see I added some debug statements to try to find out what's going on. These statements are all in a concatenated string in the first line of the error message I posted. The debug statements all give exactly what I would expect. All of them except the CameraUpdate object which has a reference but no other information alongside it. This is really baffling to me though. CameraUpdateFactory.newLatLngZoom() is passing a null object, but the parameters are exactly what it's looking for. If miniMap (Google Map Object) was not initialized than I would think it wouldn't even recognize CameraUpdateFactory. Any help on this would be greatly appreciated.

private void getDeviceLocation() {
    /*
     * Get the best and most recent location of the device, which may be null in rare
     * cases when a location is not available.
     */
    try {
        if (mLocationPermissionGranted) {
            Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
            locationResult.addOnCompleteListener(this, new OnCompleteListener<Location>() {
                @Override
                public void onComplete(@NonNull Task<Location> task) {
                    if (task.isSuccessful()) {
                        mLastKnownLocation = task.getResult();
                        //DEBUG
                        //Both give correct double
                        String errormess = Double.toString(mLastKnownLocation.getLatitude());
                        String errormesstwo = Double.toString(mLastKnownLocation.getLongitude());
                        //Gives a proper LatLng Object
                        String errormessthree = String.valueOf(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()));
                        //Gives a reference and nothing else...  Not working...
                        CameraUpdate SHYAT = CameraUpdateFactory.newLatLngZoom(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), DEFAULT_ZOOM);
                        String errormessfour = String.valueOf(SHYAT);
                        Log.d(TAG, errormess + ':' + errormesstwo + ':' + errormessthree + ':' + errormessfour);
                        miniMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                                new LatLng(mLastKnownLocation.getLatitude(),
                                        mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
                    } else {
                        Log.d(TAG, "Current location is null. Using defaults.");
                        Log.e(TAG, "Exception: %s", task.getException());
                        miniMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
                        miniMap.getUiSettings().setMyLocationButtonEnabled(false);
                    }
                }
            });
        }
    } catch(SecurityException e)  {
        Log.e("Exception: %s", e.getMessage());
    }
}

Fixed, No idea why.

In coding through the tutorial I took out

onMapReady(GoogleMap map) {

miniMap = map;

}

and replaced it with

onMapReady(GoogleMap miniMap) {}

Changing it back to onMapReady(GoogleMap map) {miniMap = map;} fixed it up. Not sure why.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM