ButterKnife 8.6.0无法正常工作

时间:2017-06-15 13:02:55

标签: android nullpointerexception butterknife

我之前使用的是7.0.1它工作正常但突然停止工作,在使用变量时给出空指针异常。然后我将其更改为8.6.0

我的代码是这样的。

(项目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'

内部碎片

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) {

        }
    });
 }
}

但是它给出了java.lang.NullPointerException(参见日志) enter image description here

注意:我仅发布了相关代码

我还查看了这些123的答案,但不起作用 它发生了两种情况(片段和活动)我做错了什么?

无法发布整个schedule_ride_fragment_layout.xml代码,所以我只发布相关代码

<?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>`

1 个答案:

答案 0 :(得分:3)

Butterknife 8.6.0开始

(项目助手)
dependencies {
       classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'
}

仅在(app gradle) build.gradle 中保留以下依赖项 ,如official documentation

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

最后, onClickListener()使用Butterknife提供的 OnClick 注释,

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