Android:ProgressDialog.show() 和getApplicationContext冲突

我不能理解为什么会有冲突发生。下边的代码没有什么问题:

mProgressDialog= ProgressDialog.show(this, "", getString(R.string.loading), true);

但是这个代码抛出异常:

mProgressDialog= ProgressDialog.show(getApplicationContext(), "", getString(R.string.loading), true);


W/WindowManager(  569): Attempted to add windowwith non-application tokenWindowToken{438bee58 token=null}.  Aborting.
D/AndroidRuntime( 2049): Shutting down VMW/dalvikvm( 2049): threadid=3: thread exitingwith uncaught exception(group=0x4001aa28)
E/AndroidRuntime( 2049): Uncaught handler: thread main exiting due to uncaught exceptionE/AndroidRuntime( 2049): java.lang.RuntimeException: Unable to start activityComponentInfo{com.tastekid.TasteKid/com.tastekid.TasteKid.YouTube}: android.view.WindowManager$BadTokenException: Unable to add window-- tokennull is not for an application
E/AndroidRuntime( 2049):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
E/AndroidRuntime( 2049):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime( 2049):    at android.app.ActivityThread.access$2100(ActivityThread.java:116)
E/AndroidRuntime( 2049):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
E/AndroidRuntime( 2049):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2049):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2049):    at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 2049):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2049):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2049):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 2049):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 2049):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2049): Caused by: android.view.WindowManager$BadTokenException: Unable to add window-- tokennull is not for an application
E/AndroidRuntime( 2049):    at android.view.ViewRoot.setView(ViewRoot.java:460)
E/AndroidRuntime( 2049):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime( 2049):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime( 2049):    at android.app.Dialog.show(Dialog.java:238)
E/AndroidRuntime( 2049):    at android.app.ProgressDialog.show(ProgressDialog.java:107)
E/AndroidRuntime( 2049):    at android.app.ProgressDialog.show(ProgressDialog.java:90)
E/AndroidRuntime( 2049):    at com.tastekid.TasteKid.YouTube.onCreate(YouTube.java:45)
E/AndroidRuntime( 2049):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
E/AndroidRuntime( 2049):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
E/AndroidRuntime( 2049):    ... 11 more

有人知道为什么会发生这个么?我从onCreate方法调用的这个。

你用的是哪个API版本?如果我的答案整好回答了你的问题,那么应该是android1.6(API版本是4)

看起来好像是object的getApplicationContext() 参数仅仅是指向了空。我想你现在遇到的问题和曾经我遇到过的一个问题很相似,在那个问题中,onCreate()中的代码在窗体确实被创建之前都运行的很好。这是一个黑客,但是尝试在几百毫秒(这个: 我用的是300-400,但是你可能需要改一下)的时间内启动一个新的线程,那将打开你的ProgressDialog然后任何你需要的都可以了(例如网络IO)。就像这样:

@Override
public void onCreate(Bundle savedInstanceState) {

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mProgressDialog = ProgressDialog.show(
               YouTube.this.getApplicationContext(), "",
               YouTube.this.getString(R.string.loading), true);

        }
    }, 1000); 
}

我正在使用android2.1版本,API7,我遇到了这个(或者是相似的)问题,通过下边的方法解决的:

Dialog dialog = new Dialog(this);

代替了这个

Dialog dialog = new Dialog(getApplicationContext());

希望这个有帮助。

对于我来说,把

builder = new AlertDialog.Builder(getApplicationContext());

改成

builder = new AlertDialog.Builder(ThisActivityClassName.this);

就好了。