Adnan Adnan - 3 days ago 6
Java Question

Unable to bind views inside fragment

I created

OnClickListener
and pass it as parameter to my
setOnBackButtonClickListener
method. It is working perfectly fine when I launch the app for first time, but when I open it for second time, I get an exception. I think it might be related to the Fragment
LifeCycle
, because I am calling method inside
onViewCreated
.

Here is exception:

Process: com., PID: 12660
java.lang.RuntimeException: Unable to bind views for com.presenter.view.fragment.ForgotPasswordFragment
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:279)


Here is my fragment:

@Bind(R.id.action_bar) ActionBar actionBar;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);


DaggerForgotPasswordComponent.builder()
.applicationComponent(
((AndroidApplication) getActivity().getApplication()).getApplicationComponent())
.forgotPasswordModule(new ForgotPasswordModule())
.build()
.inject(this);

}

View.OnClickListener cl = new View.OnClickListener() {
@Override
public void onClick(View v) {
navigateTo(LoginActivity.class);
}
};

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.actionBar.setOnBackButtonClickListener(cl);

}

Answer

Try to override onCreateView and bind like that with ButterKnife

@BindView(R.id.action_bar) ActionBar actionBar;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View fragmentView = inflater.inflate(R.layout.fragment_layout, container, false);
    ButterKnife.bind(this, fragmentView);

    return fragmentView;
}
Comments