我有一个 Login Fragment 和一个名为 CServerResponse 的类。

我想从 LoginFragment 类中调用 CServerResponse

我怎样才能做到这一点?

这是 CServerResponse 类代码:

public class CServerResponse {
    public static CServerResponse s_m_oServerResponse;
    public Context m_Context;

    private CServerResponse(Context m_Context) {
        this.m_Context = m_Context;
    }

    public static CServerResponse getInstance() {
        if (s_m_oServerResponse == null) {
            s_m_oServerResponse = new CServerResponse();
        }
        return s_m_oServerResponse;
    }

    public void getLoginResponse() throws JSONException {
        final Fragment activity = (Fragment) m_Context;
        if (CLoginScreen.m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {

            CLoginScreen.m_oLoginSession.setLoginData(
                CLoginScreen.s_szResponseMobile, CLoginScreen.s_szResponsePassword);

            getActivity().getSupportFragmentManager()
                         .beginTransaction()
                         .replace(R.id.container, new CDealMainListing()).commit();

            CToastMessage.getInstance().showToast(getActivity(), "You are successfully Logged In");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Agentcode Can Not Be Empty")) {

            CToastMessage.getInstance().showToast(getActivity(), "Please Enter Valid Mobile Number");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Pin Can Not Be Empty")) {

            CToastMessage.getInstance().showToast(getActivity(), "Please Enter Password");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Invalid PIN")) {

            CToastMessage.getInstance().showToast(getActivity(), "Please enter correct Password");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Subscriber/Agent Blocked due to Wrong Attempts")) {

            CToastMessage.getInstance().showToast(getActivity(), "You are blocked as You finished your all attempt");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {

            CToastMessage.getInstance().showToast(getActivity(), "Connection Lost ! Please Try Again");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Subscriber/Agent Not Found")) {

            CToastMessage.getInstance().showToast(getActivity(), "User not found ! Kindly Regiter before Login");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("OTP not verify")) {

            CToastMessage.getInstance().showToast(getActivity(), "Otp not Verify ! Kindly Generate Otp on Sign Up");
        }
    }
}

最佳答案

您需要访问需要为其调用 fragment 的 Activity 的上下文。

假设您在名为 startLoginFragment(Context context) 的方法中有相应 Activity 的上下文。该方法的代码如下:

public void startLoginFragmemt(Context context) {
    Activity activity = (Activity) context;
    FragmentManager fragmentManager = activity.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    LoginFragment fragment = new LoginFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}

10-06 03:19