这是我的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>
我绑定了扩展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();
}
....
}
使用此方法获取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'
通过将annotationProcessor更改为apt来解决类似problem的问题,但就我而言,它只是不会同步。
快速更新
即使我更换,问题仍然存在
@BindView(R.id.disconnected_view)RelativeLayout disconnectedView;
同
我disconnectedView = v.findViewById(R.id.disconnected_view);
方法中的onCreateView
。所以一定不是奶油刀的错误。
答案 0 :(得分:1)
在没有Butterknife
的情况下使用unbinder
,您需要返回已经夸大的视图,而不是创建新视图&amp;膨胀它。
还在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);
}