Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

MyLocationNewOverlay#disableFollowLocation and MyLocationNewOverlay#disableMyLocation throw NullPointerException if called after MyLocationNewOverlay#onDetach #1783

@seadowg

Description

@seadowg

Issue Type

Bug

Description and/or steps/code to reproduce the problem

I found this due to having some code that class disableMyLocation/disableFollowLocation in the onPause of a Fragment. For normal Fragment classes this is fine, but interestingly when using a DialogFragment View#onDetachedFromWindow (which from the MapView will eventually call onDetach on its overlays) is called for views before onPause when DialogFragment#dismiss is called. This means that onPause that tries to clean up MyLocationNewOverlay explodes in a DialogFragment as disableMyLocation and disableFollowLocation both make unchecked calls to the mMapController that has been set to null by onDetach.

I wrote an Android instrumentation test that demonstrates this:

class OsmDroidOverlayProblemTest {

    @Test
    fun example() {
        FragmentScenario.launch(DialogFragmentContainer::class.java).onFragment {
            it.dismiss() // Throws NullPointerException
        }
    }
}

class DialogFragmentContainer : DialogFragment() {

    private lateinit var myLocationNewOverlay: MyLocationNewOverlay

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return MapView(context)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val mapView = view as MapView

        myLocationNewOverlay = MyLocationNewOverlay(mapView)
        mapView.overlays.add(myLocationNewOverlay)
    }

    override fun onPause() {
        super.onPause()
        myLocationNewOverlay.disableFollowLocation()
    }
}

For the moment, we're working around it by checking View#isAttachedToWindow() for the MapView before trying to clean up the overlay.

If it's a bug, version(s) of android this affects:

All as far I've seen (tried with various API levels, but the test was running in API 29).

Version of osmdroid the issue relates to:

6.1.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions