Search code examples
androidandroid-fragmentsbutterknife

Nested viewgroup is null after binding with butterknife


Here's my fragment_photo_gallery.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/disconnected_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone">

    <ImageView
        android:id="@+id/disconnected_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_offline" />

    <TextView
        android:id="@+id/disconnected_title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/disconnected_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:fontFamily="sans-serif-medium"
        android:gravity="center"
        android:text="@string/disconnected_title_text"
        android:textAppearance="?android:textAppearanceMedium" />

    <TextView
        android:id="@+id/disconnected_subtitle_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/disconnected_title_text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:fontFamily="sans-serif"
        android:gravity="center"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="@string/disconnected_subtitle_text"
        android:textAppearance="?android:textAppearanceSmall"
        android:textColor="#A2AAB0" />

    <Button
        android:id="@+id/disconnected_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/disconnected_subtitle_text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:text="@string/disconnected_button_text" />

</RelativeLayout>

</RelativeLayout>

I bind all the stuff in the class which extends fragment:

public class PhotoGalleryFragment extends Fragment {

    ....

    @BindView(R.id.disconnected_view)
    RelativeLayout disconnectedView;

    ....

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }

    @Override public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    ....
}

And get NPE on this method:

private void showDisconnectedView() {
    disconnectedView.setVisibility(View.VISIBLE);
    progressBar.setVisibility(View.GONE);
}

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference

My build gradle looks ok so I don't know why I get such an error

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

The guy who got similar problem figured out by changing annotationProcessor to apt but in my case, it just won't ever sync.


QUICK UPDATE

The problem still happened even if I replace @BindView(R.id.disconnected_view)RelativeLayout disconnectedView; with disconnectedView = v.findViewById(R.id.disconnected_view); in my onCreateView method. So it must be not butterknife fault.


Solution

  • Use Butterknife without unbinder, You need to return already inflated view rather than creating new view & inflating it.

    Also apply view specific code in onViewCreated() method.

    public class PhotoGalleryFragment extends Fragment {
    
    @BindView(R.id.disconnected_view)
    RelativeLayout disconnectedView;
    
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
        ButterKnife.bind(this, v);
        return v;
    }
    
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        disconnectedView.setVisibility(View.GONE);
    }