Search code examples
androidandroid-intentandroid-activitynullreferenceexceptioncustomdialog

App crashes while passing Intent


I have a custom dialog activity which asks user to enter OTP. I then do the normal working of OTP process by checking if it was correct and so on.

Now when i am done with it, i want to open the next activity which i am not able to open.

My code for the Custom dialog is:

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

I get an error when i try to give an intent and start SecondActivity. The error is:

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)

I tried adding AuthenticationActivity.this,getApplicationContext(),getContext() inside the first argument of Intent. None seem to work.

A little help would be appreciated. Let me know if you need anything else


Solution

  • You can try this approach to call the second activity from the asynctask
    When initializing the object of asynctask pass the current class reference as parameter

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