我正在尝试通过GroundOverlay
使用预设雷达图像阵列的顺序 s 的演示在 Google 地图上显示渐进式雷达图像。为了不让用户界面忙碌,我为此使用了不同的线程。不幸的是,似乎正在发生奇怪NullPointerException
的事情。我已经调试了代码,并且没有找到任何可能导致此问题的空变量。
我应该怎么办?
我将只显示相关代码:(请注意,radaroverlay、map、images、image_index 和 MAP_BOUNDS 是全局变量,不为 null - 请参见1)。
编辑 - 简化了下面的代码。仍然给出完全相同的错误。
public void createLooper() {
final Runnable runnable = new Runnable() {
public void run() {
if(run == true) {
mHandler.postDelayed(this,500);
} else {
return;
}
if(radaroverlay != null) {
radaroverlay.remove();
}
// Problem happens in the line below.
try {
GroundOverlayOptions opts = new GroundOverlayOptions()
.image(images[image_index])
.positionFromBounds(MAP_BOUNDS)
.transparency((float) 0.3);
radaroverlay = map.addGroundOverlay(opts);
} catch (Exception e) {
e.printStackTrace();
run = false;
}
if(image_index == 19) {
image_index = 0;
} else {
image_index++;
}
}
};
//mHandler.postDelayed(runnable, 500);
new Thread(runnable).start();
和对应的logcat
02-27 16:58:04.815: E/AndroidRuntime(6791): FATAL EXCEPTION: main
02-27 16:58:04.815: E/AndroidRuntime(6791): java.lang.NullPointerException
02-27 16:58:04.815: E/AndroidRuntime(6791): at maps.z.aa.<init>(Unknown Source)
02-27 16:58:04.815: E/AndroidRuntime(6791): at maps.z.bi.a(Unknown Source)
02-27 16:58:04.815: E/AndroidRuntime(6791): at maps.z.bi.b(Unknown Source)
02-27 16:58:04.815: E/AndroidRuntime(6791): at maps.z.ag.addGroundOverlay(Unknown Source)
02-27 16:58:04.815: E/AndroidRuntime(6791): at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:182)
02-27 16:58:04.815: E/AndroidRuntime(6791): at android.os.Binder.transact(Binder.java:249)
02-27 16:58:04.815: E/AndroidRuntime(6791): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addGroundOverlay(Unknown Source)
02-27 16:58:04.815: E/AndroidRuntime(6791): at com.google.android.gms.maps.GoogleMap.addGroundOverlay(Unknown Source)
02-27 16:58:04.815: E/AndroidRuntime(6791): at com.aquamet.saramap.MapActivity$2$1.run(MapActivity.java:520)
02-27 16:58:04.815: E/AndroidRuntime(6791): at android.os.Handler.handleCallback(Handler.java:587)
02-27 16:58:04.815: E/AndroidRuntime(6791): at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 16:58:04.815: E/AndroidRuntime(6791): at android.os.Looper.loop(Looper.java:123)
02-27 16:58:04.815: E/AndroidRuntime(6791): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-27 16:58:04.815: E/AndroidRuntime(6791): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 16:58:04.815: E/AndroidRuntime(6791): at java.lang.reflect.Method.invoke(Method.java:521)
02-27 16:58:04.815: E/AndroidRuntime(6791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
02-27 16:58:04.815: E/AndroidRuntime(6791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
02-27 16:58:04.815: E/AndroidRuntime(6791): at dalvik.system.NativeStart.main(Native Method)
调试: