我正在编写我的第一个asyctask类,但最终会出现异常
我的asyctask类(内部类)
private class LogoutUser extends AsyncTask<Double, Void, Void>{
@Override
protected Void doInBackground(Double... params) {
validateuser logoutuser = new validateuser();
logoutuser.logOut();
return null;
}
它所称的地方
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog dialog = new ProgressDialog(homepage.this);
dialog.setMessage("Loading...");
dialog.show();
//validateuser logoutuser = new validateuser();
//logoutuser.logOut();
new LogoutUser().execute(10.00, 10.00);
Intent loginscreen = new Intent(homepage.this, Login2.class);
loginscreen.setFlags(0x04000000);
startActivity(loginscreen);
dialog.cancel();
}
});
我得到的例外
08-16 16:22:54.167: ERROR/AndroidRuntime(714): FATAL EXCEPTION: AsyncTask #1
08-16 16:22:54.167: ERROR/AndroidRuntime(714): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at java.lang.Thread.run(Thread.java:1019)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at android.os.Handler.<init>(Handler.java:121)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at android.app.Activity.<init>(Activity.java:680)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at a.b.Login2.<init>(Login2.java:40)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at a.b.validateuser.logOut(validateuser.java:127)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at a.b.homepage$LogoutUser.doInBackground(homepage.java:450)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at a.b.homepage$LogoutUser.doInBackground(homepage.java:1)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-16 16:22:54.167: ERROR/AndroidRuntime(714): ... 4 more
官方文档不符合我的要求,我开始写我的,最终有例外。请看看你是否可以提供帮助。
提前致谢。
答案 0 :(得分:2)
我认为您正在Asynctask中更新应用程序的某些UI,如果是,则将其删除
答案 1 :(得分:0)
loginscreen.setFlags(0x04000000);//this flag kills the Activity, before starting another, so dialog //is cancelled already
startActivity(loginscreen);
dialog.cancel();//this could be a problem line
删除dialog.cancel()
,另一种可能性是,
logoutuser.logOut();
上面的行可能正在启动另一个Thread,如果你这样做就要避免。找到其他方式..