我正在使用ButterKnife绑定视图元素。
这是我的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<Button
android:id="@+id/ok_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok" />
</LinearLayout>
我的片段:
class MyFragment : Fragment() {
@BindView(R.id.ok_btn)
lateinit var okBtn: Button
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
okBtn.setOnClickListener { view ->
Log.d("mytag", "ok clicked!")
}
}
}
当我运行简单的应用程序时,出现错误:
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property okBtn has not been initialized
E/AndroidRuntime( 5435): at com.my.app.MyFragment.onViewCreated(MyFragment.kt:35)
为什么会出现此错误?听起来@BindView(R.id.ok_btn)
没有初始化按钮实例变量。我想念什么?
线在哪里?