Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Released unknown bitmap reference - Setting marker in android

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.

like image 557
Scorpion Avatar asked Feb 03 '14 09:02

Scorpion


1 Answers

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;
}
like image 64
Francesco Gabbrielli Avatar answered Nov 15 '22 21:11

Francesco Gabbrielli