警报对话框和异步任务
我正在尝试在活动中使用警报对话框和异步任务,但收到以下错误
原因:java.lang.RuntimeException:无法在内部创建处理程序 尚未调用Looper.prepare()的线程
代码:
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);
}
}
i am trying to use Alert Dialog Box and Async Task in the activity and am getting the following error
Caused by: java.lang.RuntimeException: Can't create handler inside
thread that has not called Looper.prepare()
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法直接在
doInBackground()
方法内更新 UI。 (是的,如果您仍然想执行,请在doInBackground()
内的runOnUiThread()
方法中编写相同的内容)否则,请在内部执行
onPostExecute()
方法。You can't update UI inside the
doInBackground()
method directly. (Yes if you still want to execute then write the same inside therunOnUiThread()
method inside thedoInBackground()
)Otherwise, do it inside the
onPostExecute()
method.onPreexecute 和 onPostExecute 是异步任务中 UI 部分的一部分。 doInBackground 是一个单独的线程,因此 doInBackground 内完成的任何操作都需要以 ProgressUpdate 的形式处理
示例参考: 链接
反映了您需要在 doInBackground 进程之间对 UI 进行的任何更改。
the onPreexecute and onPostExecute are part of the UI parts in the Async Task.. the doInBackground is a seperate thread so any thing done inside the doInBackground needs to be handled in the form of progressUpdate
Example Reference: Link
reflects any changes you need to make to the UI inbetween the doInBackground process.