由CameraUpdateFactory引用的Null对象引起的致命错误

时间:2018-06-06 18:30:15

标签: java android google-maps

我的应用程序中不断出现此致命错误,这是由空对象引用引起的。

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

这是提供错误消息的代码。你可以看到我添加了一些调试语句来试图找出正在发生的事情。这些语句都在我发布的错误消息的第一行中的连接字符串中。调试语句都给出了我期望的结果。除了CameraUpdate对象之外的所有对象都有一个引用但没有其他信息。这对我来说真是令人困惑。 CameraUpdateFactory.newLatLngZoom()传递一个空对象,但参数正是它所寻找的。如果miniMap(谷歌地图对象)没有初始化,我认为它甚至不会识别CameraUpdateFactory。对此的任何帮助将不胜感激。

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

1 个答案:

答案 0 :(得分:0)

修正了,不知道为什么。

在编写完成的教程中我拿出了

onMapReady(GoogleMap map) {

miniMap = map;

}

并将其替换为

onMapReady(GoogleMap miniMap) {}

将其更改回onMapReady(GoogleMap map) {miniMap = map;} 解决了。不知道为什么。