ButterKnife:@BindView但出现错误,指出按钮实例变量未初始化

问题描述 投票:0回答:1

我正在使用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)没有初始化按钮实例变量。我想念什么?

android butterknife
1个回答
0
投票

线在哪里?

© www.soinside.com 2019 - 2024. All rights reserved.