MainActivity.this 与 getApplicationContext() 之间有什么不同

新手上路,请多包涵

我正在尝试 ProgressDialog。但我很困惑。

 1. pd=ProgressDialog.show(MainActivity.this, "", "Fething data");

当我使用 (MainActivity.this) 时就可以了。但

2. pd=ProgressDialog.show(getApplicationContext(), "", "Fething data");

当我使用 (getApplicationContext()) 时,它是错误的。

这个 progressDialog 有什么问题?

( MainActivity.this ) 与 ( getApplicationContext() ) 之间有什么区别

当我使用它的最佳时机?

对于 getApplicationContext() 错误是:

 04-09 15:05:37.453: E/AndroidRuntime(9980): FATAL EXCEPTION: main
04-09 15:05:37.453: E/AndroidRuntime(9980): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.Dialog.show(Dialog.java:281)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.ProgressDialog.show(ProgressDialog.java:116)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.ProgressDialog.show(ProgressDialog.java:99)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.ProgressDialog.show(ProgressDialog.java:94)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at com.example.shikkok_services.MainActivity$2.onClick(MainActivity.java:27)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.View.performClick(View.java:4204)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.view.View$PerformClick.run(View.java:17355)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.os.Handler.handleCallback(Handler.java:725)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.os.Looper.loop(Looper.java:137)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at java.lang.reflect.Method.invoke(Method.java:511)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-09 15:05:37.453: E/AndroidRuntime(9980):     at dalvik.system.NativeStart.main(Native Method)

原文由 AndyError 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 563
2 个回答

使用哪个上下文?

上下文有两种类型:

应用程序上下文 与应用程序相关联,并且在应用程序的整个生命周期中始终保持不变;它没有改变。因此,如果您正在使用 Toast ,您可以使用应用程序上下文或什至活动上下文(两者),因为 Toast 可以从应用程序中的任何位置显示并且不附加到特定窗口。但也有很多例外。一个这样的例外是当您需要使用或传递活动上下文时。

Activity 上下文 与 Activity 相关联,如果 Activity 被销毁,则可以被销毁;一个应用程序可能有多个活动(很有可能)。有时您绝对需要活动上下文句柄。例如,如果您启动一个新的 Activity ,您需要在其 Intent 中使用活动上下文,以便新启动的活动根据活动堆栈连接到当前活动。但是,您也可以使用应用程序的上下文来启动新活动,但是您需要设置标志 Intent.FLAG_ACTIVITY_NEW_TASK 以将其视为新任务。

让我们考虑一些情况:

MainActivity.this refers to the MainActivity context which extends Activity class but the base class ( Activity ) also extends Context 类,因此它可用于提供活动上下文。

getBaseContext() 提供活动上下文。

getApplication() 提供应用程序上下文。

getApplicationContext() 还提供应用程序上下文。

欲了解更多信息,请查看此 链接

原文由 Zohra Khan 发布,翻译遵循 CC BY-SA 4.0 许可协议

  • MainActivity.this 仅当您位于 MainActivity 的内部类时才有效。

  • 如果您在 MainActivity 本身,只需使用 this

  • If you are in another class entirely, you need to pass it an instance of a context from the Activity you are in.

希望这可以帮助..

原文由 Varun 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题