2010-12-02 55 views
2

在我的活動中,我打電話給webservices。所以在webservice返回結果之後,我必須顯示一個警報。由於警報是UI部分,我認爲在onPostExecute()我必須編寫警報代碼。但是當我喜歡這個錯誤即將到來時。顯示如何在調用webservice後顯示警報

錯誤:

12-02 09:59:08.508: ERROR/AndroidRuntime(451): Uncaught handler: thread main exiting 
due to uncaught exception 
12-02 09:59:08.528: ERROR/AndroidRuntime(451): 
android.view.WindowManager$BadTokenException: Unable to add window -- token 
[email protected] is not valid; is your 
activity running? 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.view.ViewRoot.setView(ViewRoot.java:456) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.view.Window$LocalWindowManager.addView(Window.java:409) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.app.Dialog.show(Dialog.java:238) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.app.AlertDialog$Builder.show(AlertDialog.java:802) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at com.myapp.android.activities.Register$PostCodeTask.onPostExecute(Register.java:291) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at  com.myapp.android.activities.Register$PostCodeTask.onPostExecute(Register.java:1) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.os.AsyncTask.finish(AsyncTask.java:416) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.os.AsyncTask.access$300(AsyncTask.java:127) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:428) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.os.Handler.dispatchMessage(Handler.java:99) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
android.os.Looper.loop(Looper.java:123) 
android.app.ActivityThread.main(ActivityThread.java:4203) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
java.lang.reflect.Method.invokeNative(Native Method) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
java.lang.reflect.Method.invoke(Method.java:521) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
12-02 09:59:08.528: ERROR/AndroidRuntime(451):  at dalvik.system.NativeStart.main(Native 
Method) 

誰能請幫忙解決我的問題。在此先感謝:)

+0

的幾種方法馬修能否請您給我們的僞代碼。 – 2010-12-02 04:54:32

+0

(請不要濫用「flag =>需要管理員注意」;這是爲了管理目的,例如刪除不適當的內容) – 2010-12-02 08:43:15

回答

1

您需要在UI線程中執行您的警報代碼。這樣做有它例如

runOnUiThread(new Runnable() { 
    public void run() { 
     Toast.makeText(MyActivity.this, "Hello there", Toast.LENGTH_LONG).show(); 
    } 
});