什么MainActivity.this VS getApplicationContext之间是不同的() [英] What is different between MainActivity.this vs getApplicationContext()

查看:216
本文介绍了什么MainActivity.this VS getApplicationContext之间是不同的()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想ProgressDialog.But我很迷惑。

1.pd = ProgressDialog.show(MainActivity.this,,Fething数据);

当我使用(MainActivity.this),然后就ok了。但

2.pd = ProgressDialog.show(getApplicationContext(),,Fething数据);

当我使用(getApplicationContext())这是错误。

有什么问题,这个progressDialog?

什么是(MainActivity.this)VS(getApplicationContext())

之间的不同

当我使用它的最佳时机?

有关getApplicationContext()错误是​​:

  04-09 15:05:37.453:E / AndroidRuntime(9980):致命异常:主要
04-09 15:05:37.453:E / AndroidRuntime(9980):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.app.Dialog.show(Dialog.java:281)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.app.ProgressDialog.show(ProgressDialog.java:116)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.app.ProgressDialog.show(ProgressDialog.java:99)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.app.ProgressDialog.show(ProgressDialog.java:94)
04-09 15:05:37.453:E / AndroidRuntime(9980):在com.example.shikkok_services.MainActivity $ 2.onClick(MainActivity.java:27)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.view.View.performClick(View.java:4204)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.view.View $ PerformClick.run(View.java:17355)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.os.Handler.handleCallback(Handler.java:725)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.os.Handler.dispatchMessage(Handler.java:92)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.os.Looper.loop(Looper.java:137)
04-09 15:05:37.453:E / AndroidRuntime(9980):在android.app.ActivityThread.main(ActivityThread.java:5041)
04-09 15:05:37.453:E / AndroidRuntime(9980):在java.lang.reflect.Method.invokeNative(本机方法)
04-09 15:05:37.453:E / AndroidRuntime(9980):在java.lang.reflect.Method.invoke(Method.java:511)
04-09 15:05:37.453:E / AndroidRuntime(9980):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
04-09 15:05:37.453:E / AndroidRuntime(9980):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-09 15:05:37.453:E / AndroidRuntime(9980):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

 要使用的背景?
 

有两种类型的背景信息:

应用程序上下文与应用程序相关联,并且将永远是整个应用程序生命周期一样 - 它不会改变。所以,如果你使用的是面包,你可以使用应用程序上下文,甚至活动上下文(两者),因为烤面包可以从任何地方在你的应用程序中显示,并没有连接到特定的窗口。但也有很多例外,当你需要使用或传递活动的背景下是个例外。

<强>活动上下文与该活动相关联,并且可以被摧毁,如果活性被破坏 - 可能有多个活动(很可能更多)与单个应用程序。有时,你绝对需要活动的上下文句柄。例如,如果你推出一个新的活动,你需要使用活动上下文的意图,使新的发射活动连接到当前的活动在活动栈的条款。但是,你可以使用应用程序的情况下也推出了新的活动,但你需要设置标志Intent.FLAG_ACTIVITY_NEW_TASK的意图把它作为一个新的任务。

让我们来看一些情况:

MainActivity.this指MainActivity上下文延伸活动类,但基类(活性)也延伸Context类,所以它可用于提供活动上下文

getBaseContext()提供的活动环境。

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

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

有关详细信息,请查看此链接

I am trying ProgressDialog.But I am confuse.

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

when I do use (MainActivity.this) then it is ok. But

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

When I do use (getApplicationContext()) it is ERROR.

What is problem for this progressDialog?

What is different between (MainActivity.this) vs (getApplicationContext())

and when I use it perfect time?

For getApplicationContext() Error is:

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)

解决方案

Which context to use?

There are two types of Context:

Application context is associated with the application and will always be same throughout the life of application -- it does not change. So if you are using Toast, you can use application context or even activity context (both) because toast can be displayed from anywhere with in your application and is not attached to a specific window. But there are many exceptions, one exception is when you need to use or pass the activity context.

Activity context is associated with to the activity and can be destroyed if the activity is destroyed -- there may be multiple activities (more than likely) with a single application. And sometimes you absolutely need the activity context handle. For example, should you launch a new activity, you need to use activity context in its Intent so that the new launching activity is connected to the current activity in terms of activity stack. However, you may use application's context too to launch a new activity but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.

Let's consider some cases:

MainActivity.this refers to the MainActivity context which extends Activity class but the base class (activity) also extends Context class, so it can be used to offer activity context.

getBaseContext() offers activity context.

getApplication() offers application context.

getApplicationContext() also offers application context.

For more information please check this link.

这篇关于什么MainActivity.this VS getApplicationContext之间是不同的()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆