gpt4 book ai didi

Android:ProgressDialog.show() 使用 getApplicationContext 崩溃

转载 作者:IT老高 更新时间:2023-10-28 13:02:25 24 4
gpt4 key购买 nike

我似乎无法理解为什么会这样。这段代码:

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

工作得很好。但是,这段代码:

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

抛出以下异常:

W/WindowManager(  569): Attempted to add window with non-application token WindowToken{438bee58 token=null}.  Aborting.
D/AndroidRuntime( 2049): Shutting down VM
W/dalvikvm( 2049): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
E/AndroidRuntime( 2049): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 2049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tastekid.TasteKid/com.tastekid.TasteKid.YouTube}: android.view.WindowManager$BadTokenException: Unable to add window -- token null 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 -- token null 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 级别 7 的 Android 版本 2.1。我遇到了这个(或类似的)问题并通过使用这个解决了:

Dialog dialog = new Dialog(this);

而不是这个:

Dialog dialog = new Dialog(getApplicationContext());

希望这会有所帮助:)

关于Android:ProgressDialog.show() 使用 getApplicationContext 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1561803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com