Java 接口侦听器在片段中变为null,即使在初始化之后也是如此

Java 接口侦听器在片段中变为null,即使在初始化之后也是如此,java,android,android-fragments,Java,Android,Android Fragments,我在activity中实现了片段侦听器,并在onAttach()方法中初始化它。在片段中,我一直在进行异步调用,并为result启动另一个活动。调用onActivityResult并处理请求代码后,我将调用活动中的侦听器实现,但侦听器始终抛出NPE。当侦听器变量从另一个活动获得结果后,它是如何变为null的,我对此感到非常困惑。 你知道这是怎么发生的吗 class ReviewFragment extends Fragment { private OnReviewContinueButtonCl

我在activity中实现了片段侦听器,并在onAttach()方法中初始化它。在片段中,我一直在进行异步调用,并为result启动另一个活动。调用onActivityResult并处理请求代码后,我将调用活动中的侦听器实现,但侦听器始终抛出NPE。当侦听器变量从另一个活动获得结果后,它是如何变为null的,我对此感到非常困惑。 你知道这是怎么发生的吗

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

}