'TileLoader failed to load tile due to mWriter being null (map shutdown?)

Good afternoon. Yesterday I decided to split my project into several tabs. Decided to use ViewPager2. In one of the fragments (tabs) I have an OpenSreetMap map. When I open it for the first time, everything works great, but if I scroll through the tab and then go back, the map becomes empty, and the message is repeating to the console: D/OsmDroid: TileLoader failed to load tile due to mWriter being null (map shutdown?)

activity_main.xml - I create a ViewPager2 widget here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>

fragment_map.xml - Fragment with the map

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapFragment">
    <org.osmdroid.views.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:visibility="visible"
        tools:ignore="MissingConstraints">
    </org.osmdroid.views.MapView>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity class, in which I launch the ViewPager2

    private lateinit var adapter: NumberAdapter
    private lateinit var viewPager: ViewPager2

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val ctx: Context = applicationContext
        Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx))
        adapter = NumberAdapter(this)
        viewPager = findViewById(R.id.pager)
        viewPager.adapter = adapter
    }

NumberAdapter class, where I create tabs with fragments

class NumberAdapter(fragment: FragmentActivity) : FragmentStateAdapter(fragment) {

    var frags: List<Fragment> = listOf()

    override fun getItemCount(): Int = 2
    override fun createFragment(position: Int): Fragment {
        var fragment: Fragment? = null
        when (position) {
            0 -> {
                fragment = ArrowFragment()
            }
            1 -> {
                fragment = MapFragment()
            }
        }
        frags+=fragment!!
        return fragment!!
    }
}

MapFragment class, fragment with a map that does not work

class MapFragment : Fragment() {

    private lateinit var map: MapView

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_map, container, false)
    }

  
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        map = view?.findViewById<MapView>(R.id.map)
        mapController = map.controller
        mapController!!.setZoom(12.0)
    }
}

Help Please =) P.S. Transleted with "Google Translate"



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source