与 butterknife 绑定后嵌套视图组为空

Nested viewgroup is null after binding with butterknife

这是我的 fragment_photo_gallery.xml 文件:

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

我绑定 class 扩展片段中的所有内容:

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();
    }

    ....
}

并通过此方法获得 NPE:

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

我的构建 gradle 看起来不错,所以我不知道为什么会出现这样的错误

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

得到相似结果的人 problem 通过将 annotationProcessor 更改为 apt 弄清楚了,但在我的情况下,它永远不会同步。


快速更新

更换后还是出现问题 @BindView(R.id.disconnected_view)RelativeLayout disconnectedView;disconnectedView = v.findViewById(R.id.disconnected_view); 在我的 onCreateView 方法中。所以一定不是butterknife的错。

在没有 unbinder 的情况下使用 Butterknife,您需要 return 已经扩充视图而不是创建新视图并扩充它。

也在 onViewCreated() 方法中应用查看特定代码。

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);
}