Search code examples
javaandroidandroid-fragmentsfragmentbutterknife

Butterknife not working on fragment


Butterknife is working well when i am using it on Activities, but not working when I am trying to use it in a fragment :

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

Solution

  • You have to return the view that you already have inflated, not inflate another view again.

    Instead of:

    return inflater.inflate(...);
    

    Do this:

    return view;