如何在调用webservice后显示警报

时间:2010-12-02 04:40:04

标签: android web-services alertdialog

在我的活动中,我正在调用webservices。因此,在webservice返回结果后,我必须显示警报。由于alert是UI部分,我认为在onPostExecute()里面我必须编写警报代码。但是,当我确实喜欢这个错误即将到来时。

显示错误:

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 
android.app.LocalActivityManager$LocalActivityRecord@4378eb50 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)

任何人都可以帮助解决我的问题。在此先感谢:)

1 个答案:

答案 0 :(得分:1)

您需要在UI线程中执行警报代码。例如,有几种方法可以做到这一点

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