传递Intent时应用崩溃

时间:2018-06-28 13:02:25

标签: android android-intent android-activity nullreferenceexception customdialog

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

现在,当我完成后,我想打开我无法打开的下一个活动。

我在“自定义”对话框中的代码是:

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()。似乎没有任何作用。

一点帮助将不胜感激。让我知道您是否还有其他需求

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

解决方案:

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