本文介绍了警告对话框和异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用警报对话框和异步任务的活动,我收到以下错误
Code:
public class loginTask extends AsyncTask<Void, Void, Void> {
public ProgressDialog loginDialog = new ProgressDialog(
LoginActivity.this);
@Override
protected void onPreExecute() {
loginDialog.setMessage("Please wait Logging in");
loginDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
if(loginUser()) {
loginDialog.dismiss();
alertbox("title", "winnn", "Okay");
} else {
loginDialog.dismiss();
alertbox("title", "message", "Okay");
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
loginDialog.dismiss();
Intent intentHome = new Intent(LoginActivity.this,
HomeActivity.class);
startActivity(intentHome);
}
}
解决方案
You can't update UI inside the doInBackground()
method directly. (Yes if you still want to execute then write the same inside the runOnUiThread()
method inside the doInBackground()
)
Otherwise, do it inside the onPostExecute()
method.
public class loginTask extends AsyncTask<Void, Void, Void>
{
public ProgressDialog loginDialog = new ProgressDialog( LoginActivity.this );
public Boolean flag;
@Override
protected void onPreExecute() {
loginDialog.setMessage("Please wait Logging in");
loginDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
if(loginUser())
flag = true;
else
flag=false;
return null;
}
@Override
protected void onPostExecute(Void unused) {
loginDialog.dismiss();
if(flag)
alertbox("title", "winnn", "Okay");
else
alertbox("title", "message", "Okay");
}
}
这篇关于警告对话框和异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!