传递 Intent 时应用程序崩溃

App crashes while passing Intent

我有一个自定义对话框 activity,要求用户输入 OTP。然后我通过检查它是否正确等来进行 OTP 过程的正常工作。

现在,当我完成它时,我想打开下一个 activity,但我无法打开它。

我的自定义对话框代码是:

public void showOTPDialog(Activity activity, String otp) {

    this.activity = activity;
    this.otp = otp;

    dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(true);
    dialog.setContentView(R.layout.verify_otp);

    final EditText etOtp = dialog.findViewById(R.id.et_otp);
    Button verify = dialog.findViewById(R.id.bt_go);
    Button resend = dialog.findViewById(R.id.bt_resend);

    verify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // verify if the otp entered was correct
        }
    });

    resend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //send new otp
        }
    });

    dialog.show();
    Window window = dialog.getWindow();
    if (window != null) {
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

}

private class insertOTP extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(activity);
        pDialog.setMessage("Kindly wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected String doInBackground(Void... params) {

        try {
            // RPS for inserting few things
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);
        pDialog.dismiss();

        if (response.equals("success")) {
            Intent intent = new Intent(activity, SecondActivity.class);
            intent.putExtra("class","1");
            startActivity(intent);
        } else {
            Toast.makeText(activity, "This user already exists...", Toast.LENGTH_SHORT).show();
        }
    }
}

当我尝试给出意图并启动 SecondActivity 时出现错误。 错误是:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
    at android.app.Activity.startActivityForResult(Activity.java:3918)
    at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
    at android.app.Activity.startActivityForResult(Activity.java:3877)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
    at android.app.Activity.startActivity(Activity.java:4200)
    at android.app.Activity.startActivity(Activity.java:4168)
    at com.globaltradingcompany.bhhopu.AuthenticateActivity$insertUser.onPostExecute(AuthenticateActivity.java:128)
    at com.globaltradingcompany.bhhopu.AuthenticateActivity$insertUser.onPostExecute(AuthenticateActivity.java:82)
    at android.os.AsyncTask.finish(AsyncTask.java:651)
    at android.os.AsyncTask.-wrap1(AsyncTask.java)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我尝试在 Intent 的第一个参数中添加 AuthenticationActivity.this,getApplicationContext(),getContext() 。 None 似乎有效。

如果能提供一点帮助,我们将不胜感激。如果您还需要什么,请告诉我

您可以尝试这种方法从 asynctask
中调用第二个 activity 初始化 asynctask 的对象时,将当前 class 引用作为参数传递

private class insertOTP extends AsyncTask<Void, Void, String> {

private Activity activity;
private ProgressDialog pDialog;

   public insertOTP(Activity activity) {
      this.activity = activity;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(activity);
        pDialog.setMessage("Kindly wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected String doInBackground(Void... params) {

        try {
            // RPS for inserting few things
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);
        pDialog.dismiss();

        if (response.equals("success")) {
            Intent intent = new Intent(activity, SecondActivity.class);
            intent.putExtra("class","1");
            activity.startActivity(intent);
        } else {
            Toast.makeText(activity, "This user already exists...", Toast.LENGTH_SHORT).show();
        }
    }
}

我找到了解决方案。我给出了基础 activity 的意图,但是从导致错误的当前 activity 开始 activity。

解决方案:

Intent intent = new Intent(activity, SecondActivity.class);
        intent.putExtra("class","1");
        activity.startActivity(intent);