为什么设置onClickListener一次可以工作? [英] Why is setting onClickListener working once?
问题描述
我想将我使用的非常牢固的项目结构迁移到Kotlin.我首先尝试了基础知识:活动和片段事务.看起来很简单:
I want to migrate a very solid project structure that I use to Kotlin. I first tried the basics: Activities and Fragment transactions. It appears so easy and simple:
class MainActivity : AppCompatActivity(), SomeInterface {
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_main)
val mainFragment = supportFragmentManager.findFragmentById(R.id.fragment_main) as MainActionsFragment?
?: MainActionsFragment.newInstance()
supportFragmentManager.inTransaction {
add(R.id.container_main, mainFragment)
}
}
private val anotherFragment by lazy {
supportFragmentManager.findFragmentById(R.id.another_fragment) as AnotherFragment?
?: AnotherFragment.newInstance()
}
override fun myInterfaceMethod() {
replaceFragment(anotherFragment, R.id.container_main)
}
}
class MainActionsFragment : Fragment() {
val btnSale: Button by bindView(R.id.btn_sale)
val btnVisit: Button by bindView(R.id.btn_visit)
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_main, container, false)
}
override fun onResume() {
super.onResume()
btnSale.setOnClickListener{ _ ->
listener.requestAction(SaleType.SALE)
}
btnVisit.setOnClickListener{ _ ->
listener.requestAction(SaleType.VISIT)
}
}
}
Extensions.kt
Extensions.kt
fun AppCompatActivity.replaceFragment(fragment: Fragment, @IdRes frameId: Int) {
supportFragmentManager.inTransaction {
replace(frameId, fragment)
.addToBackStack(fragment.tag)
}
}
inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> FragmentTransaction) {
beginTransaction()
.func()
.commit()
}
现在,MainActionsFragment
拥有两个按钮.一切都按预期进行,第一次单击将我带到所需的片段.但是,一旦按下后退"按钮并再次看到我的两个按钮,它们的单击侦听器便消失了.这几乎是翻译成Kotlin的标准处理方式,除了其出色的新功能外,没有什么花哨的.我尝试将onClickListeners设置为onCreateView()
,但崩溃了:
Now, MainActionsFragment
holds two buttons. Everything works as expected, the first click on either one takes me to the desired fragment. However, once I press the back button and I see my two buttons again, their click listener is gone. This is pretty much the standard way of doings things translated to Kotlin, nothing fancy besides its cool new features. I tried moving setting the onClickListeners to onCreateView()
but it crashes:
原因:kotlin.KotlinNullPointerException 在kotterknife.ButterKnifeKt $ viewFinder $ 7.invoke(ButterKnife.kt:95) 在kotterknife.ButterKnifeKt $ viewFinder $ 7.invoke(ButterKnife.kt) at kotterknife.ButterKnifeKt $ required $ 1.invoke(ButterKnife.kt:104) at kotterknife.ButterKnifeKt $ required $ 1.invoke(ButterKnife.kt) 在kotterknife.Lazy.getValue(ButterKnife.kt:125)
Caused by: kotlin.KotlinNullPointerException at kotterknife.ButterKnifeKt$viewFinder$7.invoke(ButterKnife.kt:95) at kotterknife.ButterKnifeKt$viewFinder$7.invoke(ButterKnife.kt) at kotterknife.ButterKnifeKt$required$1.invoke(ButterKnife.kt:104) at kotterknife.ButterKnifeKt$required$1.invoke(ButterKnife.kt) at kotterknife.Lazy.getValue(ButterKnife.kt:125)
因此,尽管Kotlin非常酷,但我在做基础工作时遇到了麻烦,并且我为迁移感到灰心.我真的在做错什么事吗?还是设置简单的点击监听器如此令人沮丧?
So, in spite of Kotlin being so cool, I'm having trouble doing the basics and I'm getting disheartened to migrate. Am I really doing things so wrong? Or how come setting a simple click listener is so frustrating?
谢谢.
推荐答案
解决方案:摆脱KotterKnife
和ButterKnife
并使用android的findViewById
解决了该问题.我猜想该语言的优势之一以及框架的进步使某些库过时或不必要.
Solution: solved it by getting rid of KotterKnife
and ButterKnife
and use android's findViewById
. I guess one of the advantages of the language and the framework's progress make some libraries obsolete or unnecessary.
这篇关于为什么设置onClickListener一次可以工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!