0

I have a seperate AsyncTask class that does some time consuming task. When the AsyncTask is called from the activity, a progress dialog is started. And is dismissed along the line if the user cancels the dialog or an error occurs while carrying out the background task. If the background process is successful, it is supposed to run verifyingUser method in that is the main Activity. This is where my problem lies. here is my code:

 public class CreateAccountTask extends AsyncTask<String, Void, String>{

 private ProgressDialog mpDialog;
 private CreateAccountTask task; 
 private Context context;
 private Activity activity;
 private CreateAccount createAccount;
 private AsyncTaskListener asyncTaskListener;


 public CreateAccountTask(Activity activity, AsyncTaskListener asyncTaskListener){

this.activity = activity;
this.asyncTaskListener = asyncTaskListener;
}


@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();

    mpDialog = new ProgressDialog(activity);

    //mpDialog.setTitle("Creating Account");
    mpDialog.setMessage("Please wait.");
    mpDialog.setCancelable(false);
    mpDialog.setIndeterminate(false);
    mpDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new      DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            //task.cancel(true);
            mpDialog.dismiss();

        }
    });

    mpDialog.show();

}


@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub

String fphoneNo = arg0[1];
String fpassword = arg0[0];

// create instance of the parseUser Class
ParseUser newUser = new ParseUser();
newUser.setUsername(fphoneNo);
newUser.setPassword(fpassword);

// here Check if progress dialog has been cancelled

if (!isCancelled()){

// if dialog has not been cancelled create the new user here
newUser.signUpInBackground(new SignUpCallback() {

    @Override
    public void done(ParseException e) {

              // Something went wrong Sorry!

        if(e != null ){


            if(!isCancelled()){
            mpDialog.dismiss();

            String errorMessage = e.getMessage().toString();

            ErrorHappened(errorMessage);

            }

            else{

                String errorMessage = "Registration Cancelled!";
                ErrorHappened(errorMessage);

            }

        }

        // No Problems 
        else {
            if (!isCancelled()) {

                // Wait for five seconds before starting the activity
                //verifyingUser();

            }
            else {
                String errorMessage = "Registration Cancelled2!";
                ErrorHappened(errorMessage);

                //Delete user in background
                ParseUser currentUser = ParseUser.getCurrentUser();
                if (currentUser != null) {
                    currentUser.deleteInBackground();
                }

            }
        }   

        }

                });

}

return null;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
asyncTaskListener.onTaskComplete(result);
}

private void ErrorHappened(String errorMessage) {

Crouton CreateMagic = Crouton.makeText(createAccount, errorMessage, CroutonClass.ALERT);
CreateMagic.setConfiguration(CroutonClass.configure);
CreateMagic.show();


}

}

Here is my main activity, where i try to dismiss the progress dialog when the verifyingUser method is called:

 public class CreateAccount extends ActionBarActivity implements AsyncTaskListener {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_account);


mPassWord = (EditText) findViewById(R.id.password);
mcreateAccount = (Button) findViewById(R.id.createAcct);
mPhoneNumber = (EditText) findViewById(R.id.Phone_Number);



// create account Method
createAccount();    

}//end of on create.


public void createAccount() {

mcreateAccount.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {


            CreateAccountTask newTask = new CreateAccountTask(CreateAccount.this, new    CreateAccount());
            newTask.execute(passwordString, fpartphoneNo);



        });

}

private void verifyingUser() {

ok();


}

protected void ok() {
    // TODO Auto-generated method stub

    CreateAccountTask.mpDialog.dismiss();

    Intent nextPage = new Intent(getBaseContext(), VerifyAccount.class);
    startActivity(nextPage);


}

@Override
public void onBackPressed() {
// TODO Auto-generated method stub

Crouton.cancelAllCroutons();
super.onBackPressed();
}

@Override
public void onTaskComplete(String result) {
// TODO Auto-generated method stub

}

}

I get this exception when it tries to dismiss the dialog:

 03-05 13:43:32.839: E/AndroidRuntime(21782): FATAL EXCEPTION: main
 03-05 13:43:32.839: E/AndroidRuntime(21782): java.lang.NullPointerException
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at android.content.ComponentName.<init>  (ComponentName.java:75)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at android.content.Intent.<init>(Intent.java:3655)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at com.example.razcat.CreateAccount.ok(CreateAccount.java:527)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at com.example.razcat.CreateAccount$3.done(CreateAccount.java:480)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at com.example.razcat.CreateAccount$3.done(CreateAccount.java:1)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at com.parse.FunctionCallback.internalDone(FunctionCallback.java:44)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at com.parse.Parse$6$1.run(Parse.java:834)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at android.os.Handler.handleCallback(Handler.java:615)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at android.os.Handler.dispatchMessage(Handler.java:92)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at android.os.Looper.loop(Looper.java:155)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at android.app.ActivityThread.main(ActivityThread.java:5454)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at java.lang.reflect.Method.invokeNative(Native Method)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at java.lang.reflect.Method.invoke(Method.java:511)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
 03-05 13:43:32.839: E/AndroidRuntime(21782):   at dalvik.system.NativeStart.main(Native Method)

How do i dismiss the dialog from the verifyingUser method?

bibi_bryan
  • 371
  • 3
  • 17

1 Answers1

0

You are trying to modify UI components within a worker thread. You have to do this in onPostExecute or within the main thread. What you can do is cancel the task or return null or some flag value, then dismiss onPostExecute.

Android - Cancel AsyncTask Forcefully

Community
  • 1
  • 1
Toguard
  • 437
  • 3
  • 9