由 CameraUpdateFactory 中的空对象引用导致的致命错误



我在我的应用程序中不断收到这个致命错误,这是由空对象引用引起的。

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(Google Map Object(没有初始化,那么我认为它甚至无法识别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());
}
}

固定,不知道为什么。

在编码过程中,我拿出了教程

onMapReady(GoogleMap map) {
miniMap = map;
}

并将其替换为

onMapReady(GoogleMap miniMap) {}

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

相关内容

最新更新