Released unknown bitmap reference - Setting marker in android
Asked Answered
N

2

11

CODE :-

    /**
     * Update marker position and icon
     */
    private void setMarker() {
        synchronized (OBJ_LOCK) {
            if (mLatitude != 0.0 || mLongitude != 0.0) {
                mRotatedBitmap = rotateBitmap(mOriginalMarkerBitmap, mBearing);

                if (mRotatedBitmap != null) {
                    mCurrentPositionIcon = BitmapDescriptorFactory
                            .fromBitmap(mRotatedBitmap);
                    mCurrentLocationMarker.position(new LatLng(mLatitude,
                            mLongitude));

                    mCurrentLocationMarker.icon(mCurrentPositionIcon);
                    mCurrentLocationMarker.anchor(0.5f, 0.5f);

                    if (currentMarker != null && mCurrentPositionIcon != null) {
                        currentMarker.setIcon(mCurrentPositionIcon);
                        currentMarker.setAnchor(0.5f, 0.5f);
                        currentMarker.setPosition(new LatLng(mLatitude,
                                mLongitude));
                    } else {
                        currentMarker = mGoogleMap
                                .addMarker(mCurrentLocationMarker);
                    }
                }
            }
        }
    }

logcat-

02-03 14:28:53.621: E/AndroidRuntime(28639): FATAL EXCEPTION: main
02-03 14:28:53.621: E/AndroidRuntime(28639): java.lang.RuntimeException: Error receiving broadcast Intent { act=medigit.rtid.MESSAGE_ACTION flg=0x10 (has extras) } in medigit.rtid.RTIDMapsActivity$RTIDBroadcastReceiver@41ccd230
02-03 14:28:53.621: E/AndroidRuntime(28639):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at android.os.Handler.handleCallback(Handler.java:615)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at android.os.Looper.loop(Looper.java:137)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at android.app.ActivityThread.main(ActivityThread.java:4744)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at java.lang.reflect.Method.invokeNative(Native Method)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at java.lang.reflect.Method.invoke(Method.java:511)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at dalvik.system.NativeStart.main(Native Method)
02-03 14:28:53.621: E/AndroidRuntime(28639): Caused by: java.lang.IllegalArgumentException: Released unknown bitmap reference
02-03 14:28:53.621: E/AndroidRuntime(28639):    at maps.aq.o.a(Unknown Source)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at maps.af.n.b(Unknown Source)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at maps.af.bm.a(Unknown Source)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at eeo.onTransact(SourceFile:204)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at android.os.Binder.transact(Binder.java:326)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at com.google.android.gms.maps.model.internal.d$a$a.i(Unknown Source)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at com.google.android.gms.maps.model.Marker.setIcon(Unknown Source)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at medigit.rtid.RTIDMapsActivity.setMarker(RTIDMapsActivity.java:493)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at medigit.rtid.RTIDMapsActivity.access$20(RTIDMapsActivity.java:478)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at medigit.rtid.RTIDMapsActivity$RTIDBroadcastReceiver.onReceive(RTIDMapsActivity.java:461)
02-03 14:28:53.621: E/AndroidRuntime(28639):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
02-03 14:28:53.621: E/AndroidRuntime(28639):    ... 9 more

Already checked following #SO Released unknown bitmap reference , but not helpful at all for me.

Northerner answered 3/2, 2014 at 9:2 Comment(2)
Can you give more details when it happens? Why is the code synchronized? Are you calling it from multiple threads?Polygon
Not from many threads but method is called from multiple places and it can be possible that it will be at same time. So i have make it synchronized. I am calling this method from BroadcastReceiver and some other places as well.Northerner
G
5

This worked for me: don't use a dead marker reference: currentMarker can be not null but at the same time might be referred to a previous map.

Always discard marker references when you don't need them (onDestroy or onPause), and always get new ones, simply don't rely only on currentMarker!=null

    ...

    if (currentMarker == null)
        currentMarker = mGoogleMap.addMarker(mCurrentLocationMarker);

    if (mCurrentPositionIcon != null) {
        currentMarker.setIcon(mCurrentPositionIcon);
        currentMarker.setAnchor(0.5f, 0.5f);
        currentMarker.setPosition(new LatLng(mLatitude,mLongitude));
    }

    ...

@Override
public void onPause() {
    currentMarker.remove();
    currentMarker = null;
}
Gelatinize answered 23/2, 2014 at 0:15 Comment(0)
S
0

Facing the same issue, I added a mMap.clear() in the onResume function of the activity. I also had the problem with the clustered markers. Creating a new clustermanager in the onResume function fixed this:

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
    mMap.clear();
    mClusterManager = new ClusterManager<ItemMarker>(this, mMap);
    ...
}

Hope this helps ...

Salvidor answered 27/7, 2015 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.