无法绑定片段内的视图

时间:2016-11-30 15:23:44

标签: java android

我创建了OnClickListener并将其作为参数传递给我的setOnBackButtonClickListener方法。当我第一次启动应用程序时,它工作正常,但是当我第二次打开它时,我得到一个例外。我认为它可能与片段LifeCycle有关,因为我在onViewCreated内调用方法。

这是例外:

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)

这是我的片段:

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

    }

1 个答案:

答案 0 :(得分:2)

尝试覆盖onCreateView并像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;
}