Istiak Morsalin
Istiak Morsalin

Reputation: 11159

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?

Upvotes: 3

Views: 4566

Answers (4)

Taras Vovkovych
Taras Vovkovych

Reputation: 4252

For the version butterknife-compiler:10.2.3 the 'kapt' instead of 'annotationProcessor' finally helped

Upvotes: 0

Istiak Morsalin
Istiak Morsalin

Reputation: 11159

I have applied kapt 'com.jakewharton:butterknife-compiler:10.2.1' before. I tried to clean up cache of Android Studio by Invalidate Cache and Restart but it did not work too. I deleted .gradle and .idea today and after re syncing the project the issue was fixed.

Upvotes: 3

S.Ambika
S.Ambika

Reputation: 302

Add following lines in gradle file under buildTypes tag

   ` lintOptions {
    disable 'RestrictedApi'
}
compileOptions {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}

and in my project gradle file

classpath 'com.android.tools.build:gradle:3.4.2'

It is working in my case in same 10.2.1 version with androidX

Upvotes: 1

Mayur Rajvir
Mayur Rajvir

Reputation: 140

Can you please update your build.gradle with below dependencies.

implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'

After updating inject the view with butterknife.

Upvotes: 1

Related Questions