Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointer Exception on Android Google Maps while updating Map

I recently included ACRA in my application, which includes a GoogleMapSupportFragment. I now get some crash reports, which I cannot reproduce on my device. The app crashes, if I want to put new Markers on the Map. Every Marker is connected with one Location, which has further informations (address, name, ....).

Here is my class which manages the GoogleMap

public SignManager(MainActivity activity, GoogleMap map) {
    mActivity = activity;
    this.mMap = map;

    initMap();
}


private void initMap() {
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setMyLocationEnabled(true);
    //...
}

This is the function, which (sometimes) throws the NullPointerException. The function updateSignsInView is called from a LocalBroadcast from an AsyncTasks, which receives an update with new Markers from the server.

public void updateSignsInView(LocationWO[] mSignArray) {

    if(mSignArray == null){
        Log.e(DEBUG_TAG,"updateSignsInView() -> mSignArray null");
        return;
    }

    resetMap();  //delete some unneeded data and do mMap.clear()

    mMarkerLocationMap = new HashMap<Marker, LocationWO>();

    for (int i = 0; i < mSignArray.length; i++) {

        //!!! this is the bad line: SignManager.java:101  
        Marker marker = mMap.addMarker(createMapMarker(mSignArray[i])); 

        mMarkerLocationMap.put(marker, mSignArray[i]);
    }

}


 //create a new Marker with given information
 private MarkerOptions createMapMarker(LocationWO mLocation) {

    MarkerOptions mMarker = new MarkerOptions()
    .position(
        new LatLng(mLocation.getmLatitude(), mLocation
            .getmLongitude()))
    .title(mLocation.getmName()
            .icon(BitmapDescriptorFactory.fromBitmap(icon));

    return mMarker;
}

Here is the LogCat I received:

USER_COMMENT=null
ANDROID_VERSION=4.1.1
APP_VERSION_NAME=0.9
BRAND=htc_europe
PHONE_MODEL=HTC One X
CUSTOM_DATA=
STACK_TRACE=java.lang.NullPointerException
at maps.aj.bk.a(Unknown Source)
at maps.aj.bk.a(Unknown Source)
at maps.aj.al.a(Unknown Source)
at bcn.onTransact(SourceFile:167)
at android.os.Binder.transact(Binder.java:326)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
at MYAPP.SignManager.updateSignsInView(SignManager.java:101)
at MYAPP.MainActivity$1.onReceive(MainActivity.java:389)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method)

I'm 99,9% sure, that mMap cannot be null. mMap is setup in the onCreate() method.

Anyone any ideas?

EDIT: setupMap function

private void setUpMapIfNeeded() {

    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map_screen)).getMap();

        if (mMap != null) {
            setUpMap();
        }
    }
}
like image 642
longi Avatar asked Jun 13 '26 15:06

longi


2 Answers

The problem here is icon passed is null.

But if the Bitmap-Icon is null, shouldn't it crash in the createMarker()method? Or will it crash later, because there is no icon tp place at the map?

Yes, it would be better if it thrown NPE directly inside GoogleMap.addMarker or BitmapDescriptorFactory.fromBitmap, but it won't happen inside your createMapMarker unless you call a method on icon.

It's not possible to tell why it is null, because there is no relevant code in the question, but I suspect that process is killed when user leaves the application and later recreated when they return.

like image 62
MaciejGórski Avatar answered Jun 15 '26 05:06

MaciejGórski


Android destroys your app when is not used for a long time or OS needs memory. If you saved your MarkerOptions in onSaveInstanceState to later tried to recreate Marker note that icon of Marker is not Parcelable thus not saved. You need to save independently icon as a Bitmap which is Parcelable and when recreating marker do addMarker(savedMarkerOptions.icon(bitmap));

like image 34
Ognjen Stanić Avatar answered Jun 15 '26 04:06

Ognjen Stanić