how to show alert after calling the webservice [duplicate]

那年仲夏 提交于 2020-01-04 06:11:25

问题


In my activity I am calling webservices. So after the webservice returns the result, I have to show an alert. Since alert is UI part, I think inside onPostExecute() I have to write the alert code. But when I do like that error is coming.

Error shown:

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)

Can anyone please help to solve my issue. Thanks in advance :)


回答1:


You need to execute your alert code in UI thread. There are few ways of doing it for example

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


来源:https://stackoverflow.com/questions/4331735/how-to-show-alert-after-calling-the-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!