ButterKnife 8.6.0无法正常工作

问题描述 投票:2回答:1

我以前使用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>`
android nullpointerexception butterknife
1个回答
3
投票

从Butterknife 8.6.0开始:

从(project gradle)中删除build.gradle中的classpath插件

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

并且只在official documentation中提到的(app gradle)中保留build.gradle中的以下依赖项

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){
}
© www.soinside.com 2019 - 2024. All rights reserved.