0
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)
4

2 回答 2

0

您正在保留对已被销毁(通常是因为您正在使用 onCreateDialog 或您将 Activity 传递给没有'当 Activity 被销毁时不会被销毁)

于 2012-10-10T12:37:57.700 回答
0

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

于 2012-10-10T12:47:15.090 回答