java - ButterKnife is not binding views and causing exceptions
问题描述
I am using Butterknife(10.2.1) to bind views in a project.I recently migrated it to AndroidX.
Though I am doing everything according to documentation here By calling
@BindView(R.id.layout_purchase_item1)
View layoutPurchaseItem1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase);
ButterKnife.bind(this);
layoutPurchaseItem1.setOnClickListener(this);
It is not working and giving me below exceptions:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.sdf.bp.billings.PurchaseActivity.onCreate(PurchaseActivity.java:108)
It is clear that the views are not binding here but I did nothing wrong in the layout file as just replacing binding with findViewById
takes care of the exception. But I want to use Butterknife. Any ideas?
解决方案
我之前申请kapt 'com.jakewharton:butterknife-compiler:10.2.1'
过。我试图通过 Invalidate Cache and Restart 清理 Android Studio 的缓存,但它也不起作用。我今天删除了 .gradle 和 .idea,在重新同步项目后问题得到了解决。
推荐阅读
- pyqt5 - 将其他函数返回值传递给 PYQt5 中的 QtableView
- tableau-api - 需要通过计算字段在表格中翻译以下简单查询
- c - 头文件中的函数如何链接到 .c 文件?
- r - 将模型摘要打印到 R 中的文件
- typescript - 在使用 Vue Composition API 和 TypeScript 时,我应该在声明作为单个文件组件的模板 ref 时使用什么类型?
- javascript - 在 AdminLTE 中使用 Node JS、Mongoose 和 Express 禁用 HEX 代码将正斜杠 (/) 转换为 /
- intel - 为什么我们在 DH 安全会话建立中需要发起者或响应者的 enclave 身份?
- reactjs - 未考虑渲染中的 if 语句
- python-3.x - UnboundLocalError:分配前引用的局部变量“playercount”
- c# - 查找正则表达式模式匹配字符串有多个条件?