Alertdialog android系统中创建例外 [英] Alertdialog creating exception in android

查看:240
本文介绍了Alertdialog android系统中创建例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用异步任务与警报对话框onPostexecute method.but我的问题是,当我异步任务completes.The Alertdialog(alert.show)之前回来previous活动是给下面的exception.The Alertdialog将出现在窗口,但为什么没有在其他屏幕显示,当我切换回来。

logcat的错误:

  5月2日至27日:03:05.283:E / AndroidRuntime(827):致命异常:主要
5月2日至27日:03:05.283:E / AndroidRuntime(827):$ android.view.WindowManager BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@40f25058无效;是您的活动运行?
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.app.Dialog.show(Dialog.java:281)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.app.AlertDialog $ Builder.show(AlertDialog.java:951)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在com.example.parentportal.Email $ emailtask.onPostExecute(Email.java:119)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在com.example.parentportal.Email $ emailtask.onPostExecute(Email.java:1)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.os.AsyncTask.finish(AsyncTask.java:631)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.os.AsyncTask.access $ 600(AsyncTask.java:177)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.os.Handler.dispatchMessage(Handler.java:99)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.os.Looper.loop(Looper.java:137)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在android.app.ActivityThread.main(ActivityThread.java:5039)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在java.lang.reflect.Method.invokeNative(本机方法)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在java.lang.reflect.Method.invoke(Method.java:511)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
5月2日至27日:03:05.283:E / AndroidRuntime(827):在dalvik.system.NativeStart.main(本机方法)

code

 上下文mContext = Email.this;
               AlertDialog.Builder警报=新AlertDialog.Builder(mContext);               alert.setTitle();
               西弗吉尼亚州的WebView =新的WebView(mContext);
               字符串的html =< H3>消息已成功&LT发送; / H3>中;               wv.loadData(HTML,text / html的,UTF-8);
               alert.setView(WV);
// alert.setPositiveButton(是,新DialogInterface.OnClickListener(){
//公共无效的onClick(DialogInterface对话,诠释的id){
//
//}
//});
               alert.setNegativeButton(取消,新DialogInterface.OnClickListener(){
                  公共无效的onClick(DialogInterface对话,诠释的id){                  }
               });
               alert.show();
        }


解决方案

这个问题是因为在onPostExecute(),您必须警告对话框,你是给电子邮件活动的背景。但你导航到其他活动,所以上下文是错误的。因此,你得到这个错误!

更好地显示一个进度并且允许用户向AsyncTask的完成执行后导航。如果你希望它在后台运行,使用服务。

i am using Alert-dialog in Asynchronous task onPostexecute method.but my problem is when i come back to previous activity before the Asynchronous task completes.The Alertdialog (alert.show) is giving the following exception.The Alertdialog will appear on window but why this is not showing in other screens when i switch back.

Logcat Error:

02-27 05:03:05.283: E/AndroidRuntime(827): FATAL EXCEPTION: main
02-27 05:03:05.283: E/AndroidRuntime(827): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@40f25058 is not valid; is your activity running?
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.app.Dialog.show(Dialog.java:281)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
02-27 05:03:05.283: E/AndroidRuntime(827):  at com.example.parentportal.Email$emailtask.onPostExecute(Email.java:119)
02-27 05:03:05.283: E/AndroidRuntime(827):  at com.example.parentportal.Email$emailtask.onPostExecute(Email.java:1)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.os.AsyncTask.finish(AsyncTask.java:631)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.os.Looper.loop(Looper.java:137)
02-27 05:03:05.283: E/AndroidRuntime(827):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-27 05:03:05.283: E/AndroidRuntime(827):  at java.lang.reflect.Method.invokeNative(Native Method)
02-27 05:03:05.283: E/AndroidRuntime(827):  at java.lang.reflect.Method.invoke(Method.java:511)
02-27 05:03:05.283: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 05:03:05.283: E/AndroidRuntime(827):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 05:03:05.283: E/AndroidRuntime(827):  at dalvik.system.NativeStart.main(Native Method)

Code

 Context mContext = Email.this;
               AlertDialog.Builder alert = new AlertDialog.Builder(mContext);

               alert.setTitle("");
               WebView wv = new WebView(mContext);
               String html = "<h3>Message has been sent successfully</h3>";

               wv.loadData(html, "text/html", "UTF-8");
               alert.setView(wv);
//             alert.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
//                public void onClick(DialogInterface dialog, int id){
//                 
//                }
//             });
               alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                  public void onClick(DialogInterface dialog, int id){

                  }
               });
               alert.show();
        }

解决方案

The problem is because in the onPostExecute(), you have the alert dialog and you are giving the context of Email Activity. But you are navigating to the other activity, so the context is wrong. Hence you are getting this error!

Better to show a progress and allow the user to navigate after the asynctask is done executing. If you want it to run in the background, use a Service.

这篇关于Alertdialog android系统中创建例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆