Adrien Chapelet
Adrien Chapelet

Reputation: 450

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

Upvotes: 0

Views: 5044

Answers (5)

Kirguduck
Kirguduck

Reputation: 796

why nobody uses unbinder?
according to documentation , if you work with fragments, you have to use 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;
    }  

and

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

Upvotes: 2

mewadaarvind
mewadaarvind

Reputation: 399

<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

Upvotes: 1

azizbekian
azizbekian

Reputation: 62199

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;

Upvotes: 5

This work for me:

...

@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ú");
...

Upvotes: 0

Charlie
Charlie

Reputation: 3104

Try waiting until you know the view has been fully constructed before altering the layout.

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

Upvotes: 3

Related Questions