在布局视图中没有使用Butterknife初始化的解决方法是在代码中添加Butterknife的初始化代码。
首先,确保已在项目的build.gradle文件中添加Butterknife的依赖项。在dependencies块中添加以下代码:
implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
接下来,在布局视图的对应Activity或Fragment中,添加Butterknife的初始化代码。在类的onCreate方法中添加以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
// 其他初始化操作
}
上面的代码中,ButterKnife.bind(this)
方法会初始化使用了Butterknife注解的视图,并将其绑定到对应的Activity或Fragment中。
最后,在布局视图中的需要使用Butterknife注解的视图上添加相应的注解。例如,在布局视图中的一个TextView上添加@BindView(R.id.textView)
注解:
@BindView(R.id.textView)
TextView textView;
这样,就完成了Butterknife的初始化和绑定操作。在代码中可以直接使用textView变量操作对应的TextView视图。
上一篇:布局是什么?