Adnan
Adnan

Reputation: 25

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

    }

Upvotes: 0

Views: 676

Answers (1)

Wojtek
Wojtek

Reputation: 1348

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

Upvotes: 2

Related Questions