此代码有问题。添加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
}
})
}
}
答案 0 :(得分:1)
此错误表示您在初始化适配器之前正在使用适配器。这可能是因为商店的回调是异步的。
解决此问题的一种方法是将MutableList
放入适配器,并使用空列表将其初始化为onCreate:
private lateinit var adapter: MyStoreRecyclerViewAdapter
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
adapter = MyStoreRecyclerViewAdapter(mutableListOf)
recyclerView.adapter = adapter
然后在调用商店的回调时:
adapter.list = stores
此外,不要忘记在设置列表后在notifyDataSetChanged()
上调用adapter
,否则您的更改将不可见
答案 1 :(得分:0)
并且,如果您不希望使用mutableList
,只需初始化ArrayList<>
private lateinit var arrayList: ArrayList<String>
为
bluetoothDevices = arrayListOf()
在onCreate()
方法中,然后将其附加到Adapter