我正在创建我的应用的用户注册部分。我正在使用AsyncTask
类在一个单独的线程上处理这项工作。我在一个单独的类文件中有我的AsyncTask
类。此
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();
}
}
在我的onPreExecute
方法中,我启动了一个progressDialog,它在执行后台任务时显示。在我的doInBackground
方法中,我获得了用户在主Activity中提供的用户名和密码,并注册了用户。这是我的问题开始的地方,如果在用户注册期间发生错误,我想使用Crouton(冷却器版本的吐司)通知用户此错误。但是当应用程序到达用crouton显示错误时崩溃了。
我的问题是:
这是我的活动:
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() {
}
private void shitHappened(String errorMessage) {
// TODO Auto-generated method stub
Crouton CreateMagic = Crouton.makeText(this, errorMessage, CroutonClass.ALERT);
CreateMagic.setConfiguration(CroutonClass.configure);
CreateMagic.show();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Crouton.cancelAllCroutons();
super.onBackPressed();
}
@Override
public void onTaskComplete(String result) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
只允许来自UIThread的通知和任何其他UI操作。如果您查看AsyncTask
的文档,doInBackground()
会在其自己的主题上运行,而onPreExecute()
,onPostExecute()
和onCancelled()
会在UIThread上运行。
在您的情况下,您应该使用protected void onCancelled(String result)
作为通知。它在UIThread上运行,可帮助您区分成功和不成功的操作。要使用此功能,请致电cancel()
并检查操作是否已取消doInBackground()
。
onCancelled()
而不是onPostExecute()
在您的特殊情况下,它可能是上下文的问题,因为您将CreateAccount.this
存储在activity
的{{1}}中的构造函数中,但您正在使用{{1 }}。尝试将其更改为以下内容:
CreateAccountTask
答案 1 :(得分:0)
使用runOnUiThread
如下...
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
runOnUiThread(new Runnable() {
@Override
public void run() {
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();
}
}