在Fragment中没有触发OnClick

时间:2016-11-12 16:41:01

标签: java android onclick

获得两个空的Activites(A和B),它只在活动A的ViewPager中保存两个片段。 我没有代码错误,一切似乎都很好。 当我在我的应用程序中午餐并点击按钮时,没有任何反应。我只想记录某些东西,但仍然没有发生任何事情。

我正在使用ButterKnife,到目前为止一切都很完美。我得到了几乎相同的片段,它表现很好,但片段B内的OnClick无法正常工作。我试图添加更多的OnClick方法,但它们都没有用于me.XML看起来不错,看起来与片段A几乎相同。

片段B并不复杂,只有三个TextViews和Button。

这是我的片段B的代码:

公共类ForgotPasswordFragmentComplete扩展了BaseFragment         实现BaseView {

private Realm realm;
private Email model;
@Bind(R.id.btn_resend)
AppCompatButton resendEmail;
@Inject
ForgotPasswordPresenter presenter;



    @OnClick(R.id.btn_resend)
    public void resendButton() {

   Log.d("ResendOnclick", "Checking OnClickMethod  ");
        Realm realm2 = getRealm();
        RealmQuery<Email> queryUserResend = realm2.where(Email.class);

        Email resultResend = queryUserResend.findFirst();
        ForgotPasswordPayload forgotPayload = new    ForgotPasswordPayload(resultResend.getUsername());
        this.presenter.subscribe(forgotPayload);

    }

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

        DaggerForgotPasswordCompletedComponent.builder()
                .applicationComponent(
                        ((AndroidApplication) getActivity().getApplication()).getApplicationComponent())
                .forgotPasswordCompletedModule(new ForgotPasswordCompletedModule())
                .build()
                .inject(this);
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
    @Override
    protected void onCreateViewWidgetInitialization(View view) {
        super.onCreateViewWidgetInitialization(view);
    }

    @Override
    public void onStart() {
        super.onStart();
        getEmail();
    }
    public void getEmail(){
        Realm realm = getRealm();
        RealmQuery<Email> queryUser = realm.where(Email.class);
        Email result1 = queryUser.findFirst();
        resendEmailTxt = (AutoResizeTextView) getView().findViewById(R.id.resend_user_email);
        if (resendEmailTxt != null) {
            this.resendEmailTxt.setText(result1.getUsername());
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        realm.close();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_forgot_password_fragment_complete, container, false);
    }
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.fragment_forgot_password_fragment_complete;
    }

1 个答案:

答案 0 :(得分:0)

您需要先绑定片段的视图对象,然后才能使用它。尝试将以下代码放在onCreateView()中:

View rootView = inflater.inflate(R.layout.fragment_forgot_password_fragment_complete, container, false);
ButterKnife.bind(this, rootView);
return rootView;