2011-08-16 91 views
0

我在寫我的第一asyctask類,但在例外結束安卓: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 

官方文檔不符合我的要求,我開始寫我的,並以異常結束。請看看你能否幫忙。

在此先感謝。

回答

2

我認爲你正在更新中Asyntask您的應用程序了一些UI,如果是的話然後將其刪除

+0

註銷用戶任務不涉及任何UI的東西只調用Web服務註銷。它響鈴嗎? –

+0

validateuser.logOut(validateuser.java:127) 引用此行validateuser的第127行 –

+0

yep dat幫助:) –

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

的上線可能會啓動另一個線程,避免如果你這樣做。找到其他方式..

+0

謝謝,但它沒有奏效。我用下面的答案解決了這個問題。 –