Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error lateinit property adapter has not been initialized after come back from activity

I've a problem with this code. After adding the code for the SearchView, every time that I click on one element going to StoreActivity, when I come back I receive the error

kotlin.UninitializedPropertyAccessException: lateinit property adapter has not been initialized

on row

adapter.filter.filter(newText)

How I can resolve this error? I don't understand...

class HomeFragment : BaseFragment() {

    private lateinit var adapter: MyStoreRecyclerViewAdapter
    private var stores: List<StoreRealm> = ArrayList()

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

        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onResume() {
        super.onResume()
        setHasOptionsMenu(true)

        if (isAdded) {
            val systemService = this.context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
            BLocationManager(this.context).getLocation {
                loadHome()
                if (!systemService.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    showEnableGpsDialog()
                } else {
                    gpsWarning.visibility = View.GONE
                }
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
        inflater?.inflate(R.menu.search_store, menu)
        val search: MenuItem = menu!!.findItem(R.id.searchStore)
        val searchView: SearchView = search.actionView as SearchView
        search(searchView)
        searchView.maxWidth = Int.MAX_VALUE

        super.onCreateOptionsMenu(menu, inflater)
    }

    private fun loadHome() {
        loading_spinner?.visibility = View.VISIBLE
        StoreService.getStores(
            AuthManager.getMe()?.id!!, 0, BLocationManager.lat, BLocationManager.lon
        ) { response ->
            loading_spinner?.visibility = View.GONE
            if (response.getResult() != null && response.getResult()?.isNotEmpty()!!) {
                no_data_view?.visibility = View.GONE
                store_view?.visibility = View.VISIBLE

                stores = response.getResult()!!
                // set up the RecyclerView
                val recyclerView = view.store_recycler
                recyclerView.layoutManager = GridLayoutManager(this.context, 1)
                adapter = MyStoreRecyclerViewAdapter(stores) {
                    val intent = Intent(this.context, StoreActivity::class.java)
                    intent.putExtra("store_id", it.id)
                    startActivity(intent)
                }
                recyclerView.adapter = adapter
            } else {
                no_data_view?.visibility = View.VISIBLE
                store_view?.visibility = View.GONE
            }

            if ((response.getError() as? UnknownHostException) != null) {
                AuthManager.logout(this.activity)
                NavigationManager.goToLogin(this.activity)
            }
        }
    }

    private fun search(searchView: SearchView) {
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                adapter.filter.filter(newText)

                return true
            }

        })
    }
}
like image 508
Dennis A. Boanini Avatar asked Oct 17 '22 05:10

Dennis A. Boanini


1 Answers

This error means that you're using the adapter before initializing it. Here it's probably because your store's callback is asynchronous.

One way to fix this problem is to put a MutableList in your adapter and initialize it onCreate with an empty list:

private lateinit var adapter: MyStoreRecyclerViewAdapter


override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        adapter = MyStoreRecyclerViewAdapter(mutableListOf())
        recyclerView.adapter = adapter
}

Then when your Stores' callback is called:

adapter.list = stores

Also, don't forget to call notifyDataSetChanged() on the adapter after setting the List otherwise your changes won't be visible

like image 91
Benoit TH Avatar answered Oct 30 '22 23:10

Benoit TH