У меня проблема с этим кодом.После добавления кода для SearchView каждый раз, когда я нажимаю на один элемент, идущий в StoreActivity, когда я возвращаюсь, я получаю ошибку
kotlin.UninitializedPropertyAccessException: адаптер свойства lateinit не был инициализирован
в строке
adapter.filter.filter (newText)
Как устранить эту ошибку?Я не понимаю ...
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
}
})
}
}