我正在尝试在Google Maps v2中设置标记图标。
我正在通过网络下载一些图像,并在代码中更改它们的背景。之后,我将它们设置为标记的图标。最初创建 map 时,它可以正常工作,但旋转后会出现异常。
我在以下版本运行的Android版本:4.3
我的代码如下:
UrlImageViewHelper.loadUrlDrawable(TuvaletlerMapActivity.this,
iconUrl, new UrlImageViewCallback() {
@Override
public void onLoaded(ImageView iv, Bitmap bm,
String arg2, boolean arg3) {
Bitmap bitmap = VenuesHelper.iconizeBitmap(bm);
marker.setIcon(BitmapDescriptorFactory
.fromBitmap(bitmap));
}
});
VenuesHelper.iconizeBitmap()
如下:public static Bitmap iconizeBitmap(Bitmap bm) {
Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
bm.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.parseColor("#33B5E5"));
canvas.drawBitmap(bm, 0, 0, null);
return bitmap;
}
堆栈跟踪如下:
08-07 10:16:50.684: E/AndroidRuntime(19001): FATAL EXCEPTION: main
08-07 10:16:50.684: E/AndroidRuntime(19001): java.lang.IllegalArgumentException: Released unknown bitmap reference
08-07 10:16:50.684: E/AndroidRuntime(19001): at maps.as.i.a(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at maps.ah.o.b(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at maps.ah.bn.a(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at bix.onTransact(SourceFile:204)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.Binder.transact(Binder.java:347)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.google.android.gms.internal.dm$a$a.f(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.google.android.gms.maps.model.Marker.setIcon(Unknown Source)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.behlul.tuvaletbul.TuvaletlerMapActivity$TuvaletliYukleCallbacks$1.onLoaded(TuvaletlerMapActivity.java:250)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$2.run(UrlImageViewHelper.java:615)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.onPostExecute(UrlImageViewHelper.java:653)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.koushikdutta.urlimageviewhelper.UrlImageViewHelper$3.onPostExecute(UrlImageViewHelper.java:1)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.os.Looper.loop(Looper.java:137)
08-07 10:16:50.684: E/AndroidRuntime(19001): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-07 10:16:50.684: E/AndroidRuntime(19001): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 10:16:50.684: E/AndroidRuntime(19001): at java.lang.reflect.Method.invoke(Method.java:525)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-07 10:16:50.684: E/AndroidRuntime(19001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-07 10:16:50.684: E/AndroidRuntime(19001): at dalvik.system.NativeStart.main(Native Method)
最佳答案
我有一个类似的问题,当我尝试重新加载执行“myMarker.setIcon()”的标记时,经过一些刷新后,应用程序遇到了“java.lang.IllegalArgumentException:已发布的未知位图引用”。
我发现调用“myMap.clear()”来清理所有标记是个问题,实际上,在函数docs中,您可以看到“从 map 上删除所有标记,折线,多边形,覆盖等”。
好吧,“etc”似乎比我预期的要多。
为了解决这个问题,我使用了一个自定义函数来遍历保存在HashMap中的所有标记,并逐个删除它们,仅此而已,在我的代码中再也没有像这样的异常了。
您可以遍历所有标记以将其删除,如下所示:
/**
* Alternative to myMap.clear() to avoid undesired exceptions
*/
private void clearAllMapMarkers() {
// Clearing the current map markers being shown
// Note that we do not use myMap.clear() because that incur in the exception
// "java.lang.IllegalArgumentException: Released unknown bitmap reference"
try {
for (Map.Entry<String, Marker> markerEntry : mMarkerList.entrySet()) {
markerEntry.getValue().remove();
}
} catch (IllegalArgumentException e) {
// Manage here the exception (never raised but who knows...)
}
}