android butterknife 控件都返回null
·
此问题原因之一可能是layout页面中,存在include引用块,butterknife又初始化的引用块布局中的最外层布局,导致的。
问题代码:
layout代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="@+id/include_title"
layout="@layout/include_title"/>
</androidx.constraintlayout.widget.ConstraintLayout>
include引用布局:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:id="@+id/cl_title_layout"
android:paddingLeft="@dimen/dimen_10_dp"
android:paddingRight="@dimen/dimen_10_dp"
>
<TextView
android:id="@+id/tv_title_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:padding="@dimen/dimen_5_dp"
style="@style/AppTheme.TitleTextStyle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:clickable="true"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity中的代码:
public class TestActivity extends BaseActivity {
@BindView(R.id.cl_title_layout)
ConstraintLayout mCl_title_layout //此处初始化为NULL
}
只需要将activity中的控件id改为include的id即可:
public class TestActivity extends BaseActivity {
@BindView(R.id.include_title)
ConstraintLayout mCl_title_layout //此处初始化有值了
}
更多推荐


所有评论(0)