即使在初始化之后,接口侦听器也会在片段中变为空

时间:2016-03-21 03:05:48

标签: java android android-fragments

我在活动中实现了片段监听器,并在onAttach()方法中初始化它。在片段中,我一直在进行异步调用,并为结果启动另一个活动。在调用onActivityResult并处理请求代码之后,我在活动中调用侦听器实现,但是侦听器总是抛出NPE。我非常困惑地知道在从另一个活动获得结果后,监听器变量如何变为空。 关于它如何发生的任何想法?

class ReviewFragment extends Fragment {
private OnReviewContinueButtonClickListener mListener;
/* many other variables */

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CODE_INSTAMOJO_PAYMENT) {
            mListener.onContinueButtonClick(mailId);
            showSuccessScreen(mailId);
        }
        if (requestCode == PaytmWalletManager.REQUEST_CODE_REGISTER_PAYTM_WALLET || requestCode == PaytmWalletManager.REQUEST_CODE_ADD_MONEY) {
            initiatePaytmPayment();
        }
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnReviewContinueButtonClickListener) {
        mListener = (OnReviewContinueButtonClickListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnReviewContinueButtonClickListener");
    }

}
 /* im calling this function middle of fragment from asynchronous network call onsuccess method without runonuithread() */
 private void pay(JSONObject paymentObject) throws JSONException {

    Intent payment = new Intent(getActivity().getApplicationContext(), WebviewActivity.class);
    Bundle bundle = new Bundle(3);
    bundle.putString(WebviewActivity.TITLE, "Make Payment");
    startActivityForResult(payment, REQUEST_CODE_INSTAMOJO_PAYMENT);
    hideProgressDialog();

}

 // on calling this method from asynchronous network result onSuccess()
 private void registerForFree(String orderId, String paymentId) {
    AppEventTracker.logEvent("register for free clicked", "success", "Flight Booking");

    getApp().getAccountManager().registerFreeTicketPatchCall(orderId, paymentId, getFreeTicketRegisterBody(), new Callback<JsonElement>() {
        @Override
        public void success(JsonElement jsonElement, Response response) {
            if (getActivity() == null) return;
            hideProgressDialog();
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mListener.onContinueButtonClick(mailId); // mlistener is null here even though it has been initialized on onAttach(). 
                }
            });
        }

        @Override
        public void failure(RetrofitError error) {
            if (getActivity() == null) return;
                    hideProgressDialog();
                    if (showErrorDialog) {
                        showErrorDialog = false;
                        showPriceChangeDiaglog("Error in payment generation");
                    }
                    AppEventTracker.logEvent("register for free clicked", "failure", "Explara buy Ticket");


        }
    });
}

实现上述片段的活动是

class booking extends Activity implements ReviewFragment.OnReviewContinueButtonClickListener{

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == PaytmWalletManager.REQUEST_CODE_REGISTER_PAYTM_WALLET || requestCode == PaytmWalletManager.REQUEST_CODE_ADD_MONEY) {
            ReviewItineraryFragment fragment = (ReviewItineraryFragment) getFragmentManager().findFragmentByTag("ReviewItinerary");
            fragment.initiatePaytmPayment();
        }
    }
}

@Override
public void onContinueButtonClick(String mail) {
        showSuccessScreen(mail);
}

private void showSuccessScreen(String mail) {
    if (this != null && !this.isFinishing()) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setTitle("Order Confirmation");
        FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
        String email = mail;
        String type = "Order";
        transaction.replace(R.id.container, PaymentSuccess.newInstance(email, type));
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

}

0 个答案:

没有答案