gpt4 book ai didi

android - 有时属性未初始化

转载 作者:行者123 更新时间:2023-12-02 07:32:16 24 4
gpt4 key购买 nike

我正在尝试处理FragmentPagerAdapter中的片段单击,但有时会出现致命异常:kotlin.UninitializedPropertyAccessException,它表示单击监听器属性未初始化。

所以这是PagerAdapter的代码

class ApplicationListPagerAdapter(
fm: FragmentManager,
private val onListItemClick: (isSent: Boolean, application: Application) -> Unit,
private val onGoToScholarshipsTabClicked: () -> Unit,
private val onGoToPicksTabClicked: () -> Unit
): FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT),
ApplicationListFragment.ClickListener {

override fun getCount(): Int {
return 2
}

override fun getItem(position: Int): Fragment {
val isSent = position == 1
val fragment = ApplicationListFragment.newInstance(isSent)
fragment.setApplicationSelectListener(this)

return fragment
}

override fun getPageTitle(position: Int): CharSequence? {
return if (position == 0) "PICKS" else "SENT"
}

override fun applicationSelected(isSent: Boolean, application: Application) {
onListItemClick(isSent, application)
}

override fun goToScholarshipsTabClicked() {
onGoToScholarshipsTabClicked()
}

override fun goToPicksTabClicked() {
onGoToPicksTabClicked()
}
}

编码如何片段化初始化
private lateinit var clickListener: ClickListener

fun setApplicationSelectListener(clickListener: ClickListener) {
this.clickListener = clickListener
}

interface ClickListener {

fun applicationSelected(isSent: Boolean, application: Application)
fun goToScholarshipsTabClicked()
fun goToPicksTabClicked()
}

onClick 回调中,某些用户有时会崩溃。
private fun initRecyclerView(applications: List<Application>) {
application_list_recyclerView.adapter = ApplicationListItemsAdapter(
context!!,
applications.toMutableList(),
isSent,
this,
applicationViewModel.applicationService,
onClick = {
clickListener.applicationSelected(isSent, it)
},
onDelete = { application: Application, count: Int ->
scholarshipViewModel.unFavoriteScholarship(application.scholarship)
sharedViewModel.deletePickedScholarship(application.scholarship)
applicationSharedViewModel.updatePicksCount(count)

if (count == 0) {
showNoApplicationsFragment()
}
},
onUndoDelete = { application: Application, count: Int ->
sharedViewModel.undoDeletedScholarship(application.scholarship)
applicationSharedViewModel.updatePicksCount(count)

if (count == 1) {
hideNoApplicationsFragment()
}
}
)

application_list_recyclerView.layoutManager = LinearLayoutManager(context!!)
}

感谢您的建议,希望我的解释能引起大家的共鸣。

最佳答案

界面

interface OnItemClickListener {
//just a random method
fun itemClicked(positon: Int)
}

适配器内部
private lateinit var listener: OnItemClickListener
fun attachListener(listener: OnItemClickListener){
this.listener = listener
}

Activity 度/片段
class MainActivity(): AppCompatActivity(), OnItemClickListener{
// implement interface in case you wanna use methods...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)

// calling the listener method
myAdapter.attachListener(this)
}
}

在myAdapter之上的是您的适配器必须首先与适配器类关联的init

关于android - 有时属性未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60205666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com