Ajeet Singh
Ajeet Singh

Reputation: 2009

ButterKnife 8.6.0 not working

I was previously using 7.0.1 it was working fine but suddenly it stop working, giving null pointer exception while using variables.Then i change it to 8.6.0

My code is like this.

(project gradle)

dependencies {
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'    
}

(app gradle)

apply plugin: 'com.jakewharton.butterknife'

compile 'com.jakewharton:butterknife:8.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'

Inside Fragment

public class ScheduleRideFragment extends Fragment {
@BindView(R.id.scheduleRideFragApplyCouponButton)
Button applyCouponButton;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = LayoutInflater.from(getActivity()).inflate(R.layout.schedule_ride_fragment_layout, null);
    ButterKnife.setDebug(true);
    ButterKnife.bind(this, view);
    initAndSetListners();
    return view; 
}
private void initAndSetListners() {
applyCouponButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
 }
}

but it giving java.lang.NullPointerException(see the log) enter image description here

Note: I have posted the related code only

i have also look these 1,2,3 answers but don't work it is happening Both case (Fragment and activity) am i doing somthing wrong ?

I can't Post whole schedule_ride_fragment_layout.xml code so i am posting related code only

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout 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="wrap_content">
<LinearLayout
android:id="@+id/scheduleRideCouponButtonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp"
android:visibility="invisible">
<Button
android:id="@+id/scheduleRideFragApplyCouponButton"
android:layout_width="wrap_content"
android:layout_height="35sp"
android:background="@color/brightGreen"
android:ems="12"
android:text="APPLY COUPON"
android:textColor="@color/textColor" /></LinearLayout></android.support.design.widget.CoordinatorLayout>`

Upvotes: 2

Views: 1859

Answers (1)

Prokash Sarkar
Prokash Sarkar

Reputation: 11873

Starting from Butterknife 8.6.0:

Remove classpath plugin in build.gradle from (project gradle)

dependencies {
       classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'
}

And only keep the following dependencies in build.gradle from (app gradle) as mentioned in the official documentation

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

Finally for onClickListener() use the OnClick annotation provided by Butterknife,

@OnClick(R.id.scheduleRideFragApplyCouponButton)
public void onApplyCouponButtonClick(Button button){
}

Upvotes: 3

Related Questions