Butterknife 不适用于片段

Butterknife not working on fragment

当我在 Activity 中使用 Butterknife 时它运行良好,但当我尝试在片段中使用它时它不起作用:

public class SettingsFragment extends Fragment {
private static final String TAG = "SettingsFragment";

@BindView(R.id.settings_email)      TextView _settingsMail;
@BindView(R.id.settings_password)   TextView _passwordMail;
@BindView(R.id.settings_token)      TextView _tokenMail;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //returning our layout file
    final View view = inflater.inflate(R.layout.fragment_3_settings, container, false);
    ButterKnife.bind(this, view);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
    String email = prefs.getString("email", null);
    String passwd = prefs.getString("password", null);
    String token = prefs.getString("token", null);
    _settingsMail.setText(email);
    _passwordMail.setText(passwd);
    _tokenMail.setText(token);
    return inflater.inflate(R.layout.fragment_3_settings, container, false);
}

您必须 return 您已经膨胀的 view,而不是再次膨胀另一个视图。

而不是:

return inflater.inflate(...);

这样做:

return view;

尝试等到您知道视图已完全构建后再更改布局。

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //returning our layout file
    final View view = inflater.inflate(R.layout.fragment_3_settings, container, false);
    ButterKnife.bind(this, view);
    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
    String email = prefs.getString("email", null);
    String passwd = prefs.getString("password", null);
    String token = prefs.getString("token", null);

    _settingsMail.setText(email);
    _passwordMail.setText(passwd);
    _tokenMail.setText(token);
}

这对我有用:

...

@BindView(R.id.text_input)
TextView text_input;

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

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    text_input.setText("Hello Lima, Perú");
...
<ImageView
   android:layout_marginTop="@dimen/padding_top_8dp
   android:layout_centerHorizontal="true"
   android:tint="@color/colorPrimary"
   android:padding="@dimen/padding_top_8dp"
   android:src="@drawable/ic_scan_qr_code"
   android:id="@+id/qr__code_icon_iv"
   android:background="@drawable/circle_shape_white"
   android:layout_width="40dp"
   android:layout_height="40dp" />

remove @id/qr__code_icon_iv

add @id/qr_code_icon_iv

为什么没人用unbinder?
根据 documentation ,如果你使用片段,你必须使用 unbinder

private Unbinder unbinder;        

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate...;
          unbinder = ButterKnife.bind(this, view);
        return view;
    }  

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