从另一个线程和类中删除进度Dialog

时间:2014-03-05 12:48:13

标签: java android android-asynctask

我有一个单独的AsyncTask类,它会执行一些耗时的任务。从活动中调用AsyncTask时,将启动进度对话框。如果用户取消对话框或在执行后台任务时发生错误,则沿着该行被解雇。如果后台进程成功,则应该运行verifyingUser方法,即主Activity。这就是我的问题所在。这是我的代码:

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


}

}

这是我的主要活动,我在调用verifyingUser方法时尝试关闭进度对话框:

 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

}

}

我在尝试关闭对话框时遇到此异常:

 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)

如何从verifyingUser方法中删除对话框?

1 个答案:

答案 0 :(得分:0)

您正在尝试修改工作线程中的UI组件。您必须在onPostExecute或主线程中执行此操作。你可以做的是取消任务或返回null或一些标志值,然后解除onPostExecute。

Android - Cancel AsyncTask Forcefully