Search code examples
javaandroidviewbutterknife

Butterknife not working AT ALL in my Android application


This is a very odd behaviour and I cannot understand why, basically Butterknife is NOT binding any views at all. Please see below code:

This is a Fragment:

@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
}

A very simple code! But if I start the application, it will fail with NullPointerException complaining view1 is NULL. This is very frustrating as I am doing exactly what the API Doc tells me to do.

Anyone have clues on what's going on here? Below is the 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>

And the logcat output:

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$1.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)

Solution

  • You should add the Butterknife library as follows:

    You have to add in your dependencies: apt 'com.jakewharton:butterknife-compiler:8.0.1'

    And also to put bellow the apply plugin: 'com.android.application': apply plugin: 'com.neenbedankt.android-apt'

    https://github.com/JakeWharton/butterknife