Butterknife 在我的 Android 应用程序中根本不起作用

Butterknife not working AT ALL in my Android application

这是一个非常奇怪的行为,我不明白为什么 Butterknife 根本 不绑定 任何视图。请看下面的代码:

这是一个片段

@BindView(R.id.share_detail_swipe)
public SMSwipeRefreshLayout view1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.view_share_details, container, false);

    ButterKnife.bind(this, view);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    view1.setRefreshing(true); // NPE here
}

一个非常简单的代码!但是,如果我启动该应用程序,它将失败并显示 NullPointerException 抱怨 view1NULL。这非常令人沮丧,因为我正在做 API Doc 告诉我做的事情。

有人知道这里发生了什么吗?下面是 view_share_details.xml:

<com.*.presentation.view.smwidget.SMSwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/share_detail_swipe"
tools:context="com.*.fragment.ShareDetailsFragment"
>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/detail_share_thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/detail_share_name_tv"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/detail_share_username_tv"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/detail_share_date_tv"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/detail_share_description_tv"
            />
    </LinearLayout>

</ScrollView>

</com.*.presentation.view.smwidget.SMSwipeRefreshLayout>

并且 logcat 输出:

FATAL EXCEPTION: main
Process: com.joelmin.sharemon, PID: 4981
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.*.presentation.view.smwidget.SMSwipeRefreshLayout.setRefreshing(boolean)' on a null object reference
    at com.joelmin.sharemon.presentation.fragment.ShareDetailsFragment.showRefreshing(ShareDetailsFragment.java:83)
    at com.joelmin.sharemon.presentation.presenter.ShareDetailsPresenter.fetchShare(ShareDetailsPresenter.java:25)
    at com.joelmin.sharemon.presentation.fragment.ShareDetailsFragment.onViewCreated(ShareDetailsFragment.java:102)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
    at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:517)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6843)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

您应该按如下方式添加 Butterknife 库:

您必须添加依赖项: apt 'com.jakewharton:butterknife-compiler:8.0.1'

还要将应用插件放在下面:'com.android.application': 应用插件:'com.neenbedankt.android-apt'

https://github.com/JakeWharton/butterknife

您必须在应用的 build.grade 文件中使用 kapt 而不是 annotationProcessor

使用:

kapt 'com.jakewharton:butterknife-compiler:10.2.3'

而不是:

annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'

阅读此处了解更多信息: https://github.com/JakeWharton/butterknife#download