首页 > 解决方案 > 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?

标签: javaandroidbutterknife

解决方案


我之前申请kapt 'com.jakewharton:butterknife-compiler:10.2.1'过。我试图通过 Invalidate Cache and Restart 清理 Android Studio 的缓存,但它也不起作用。我今天删除了 .gradle 和 .idea,在重新同步项目后问题得到了解决。


推荐阅读