TokenException:无法添加窗口?

时间:2012-10-10 12:31:28

标签: android progressdialog

private void runDialog(final int seconds){
        new Thread(new Runnable(){
          public void run(){
            try {
                    Thread.sleep(seconds * 1000);
                    progressDialog.dismiss();
            runOnUiThread(new Runnable()
            {
              public void run()
              {
                        // Your Alert Code
                        AlertDialog connection = new AlertDialog.Builder(
                                NewsScreenActivity.this)
                                .setTitle("No Network Found")
                                .setMessage(
                                        "service unavailable, please try again later")
                                .setPositiveButton("Ok",
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) {

                                              finish();
                                        //        progressDialog.dismiss();
                                            }
                                        }).create();

                        connection.show();
              }
            });

          }catch (InterruptedException e) {
              Log.i("=================== Connection TimeO5555555555", "======");
             // Toast.makeText(getBaseContext(), "service unavailable, please try again later", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
          }
          }
        }).start();
  }

错误

    10-10 17:55:13.299: E/AndroidRuntime(11643): FATAL EXCEPTION: main
    10-10 17:55:13.299: E/AndroidRuntime(11643): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@40531320 is not valid; is your activity running?
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.view.ViewRoot.setView(ViewRoot.java:528)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.view.Window$LocalWindowManager.addView(Window.java:424)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.app.Dialog.show(Dialog.java:241)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at com.MariTimeGlobalNews.NewsScreenActivity$7$1.run(NewsScreenActivity.java:587)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.os.Handler.handleCallback(Handler.java:587)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.os.Handler.dispatchMessage(Handler.java:92)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.os.Looper.loop(Looper.java:123)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at android.app.ActivityThread.main(ActivityThread.java:3729)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at java.lang.reflect.Method.invokeNative(Native Method)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at java.lang.reflect.Method.invoke(Method.java:507)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
    10-10 17:55:13.299: E/AndroidRuntime(11643):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

您正在继续引用已被销毁的Context(显式地,或者通过创建Dialog或Toast或其他一些依赖项)(通常是因为您正在使用onCreateDialog或者您将Activity传递给其他人)活动被销毁时没有被破坏的过程)

答案 1 :(得分:0)

线程中的上下文问题。 所以请使用Handler。 http://developer.android.com/reference/android/os/Handler.html