安卓:为什么一定要用getBaseContext()而不是此 [英] Android: why must use getBaseContext() instead of this
问题描述
这
经常引用当前上下文。但是,在某些情况下,我们为什么一定要用 getBaseContext()
而不是这
。 (这意味着,当使用这
会发现错误)。
下面是我的例子:
微调微调=(微调)findViewById(R.id.spinner);
spinner.setAdapter(适配器);
spinner.setOnItemSelectedListener(新OnItemSelectedListener(){
@覆盖
公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
Toast.makeText(getBaseContext(),选中,Toast.LENGTH_SHORT).show(); //这一行
}
在上面code,当我改变 getBaseContext()
到这
将收到错误。
谁能给我解释一下,谢谢。
-
getApplicationContext()
返回整个应用程序生命周期的应用程序环境中,当应用程序会破坏那么它也会破坏。 -
这
的背景下返回活动的当前背景下,属于活动,该活动被破坏,然后它会破坏also.but在你的情况下,意志是指微调
实例,因为我们是在onItemSelected使用这个(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3)
方法,这是由微调
类和微调
继承AdapterView.OnItemSelectedListener
接口 -
getBaseContext()
是ContextWrapper
的方法。而ContextWrapper
是进行代理执行上下文的,仅仅代表其所有调用另一个上下文。可以被子类修改行为,而不改变原有的语境。(按的javadoc)。
和你的情况:微调
类不是子背景
或 ContextWrapper
类*
Toast.makeText(getBaseContext(),选中,Toast.LENGTH_SHORT).show();
办法 getBaseContext()
是 ContextWrapper
和 ContextWrapper $ C $的方法C>是通过代理执行
上下文
所以间接地我们传递一个上下文类对象。
或者我们也可以通过Activity.this,因为活动
类 ContextWrapper
类的子类。
如果你去安卓机制的文档则此方法需要一个Context类对象:
公共静态吐司makeText(上下文的背景下,INT渣油,INT持续时间)
所以我们不能够通过一个活动或类上下文手段这
到 Toast.makeText
这别吨有一个子类,无论是上下文
或 ContextWrapper
类。
this
often to reference to current context. But, at some case, why we must use getBaseContext()
instead of this
. (It means when use this
will notice error).
Here is my example:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){
Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show(); //this line
}
At above code, when I change getBaseContext()
to this
will receive error.
Who can explain for me, please.
getApplicationContext ()
returns the application context of the entire application life cycle,when application will destroy then it will destroy also.this
the context returns the current context of the activity, belong to the activity, the activity is destroyed then it will destroy also.but in your case it will refers to theSpinner
instance because we are using this withinonItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3)
method which is fromSpinner
class andSpinner
inherit this method fromAdapterView.OnItemSelectedListener
interfacegetBaseContext()
is the method ofContextWrapper
. AndContextWrapper
is, "Proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context." (as per javadocs)..
and in your case :Spinner
class is not subclass of Context
or ContextWrapper
class*
Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show();
means getBaseContext()
is method of ContextWrapper
and ContextWrapper
is Proxying implementation of Context
so indirectly we are passing an Context Class Object.
or we can also pass 'Activity.this' because Activity
class is subclass of ContextWrapper
class .
if you go with android documention then this method require an Context class object:
public static Toast makeText (Context context, int resId, int duration)
so we are not able to pass an activity or class context means this
to Toast.makeText
which don't have a subclass of either Context
or ContextWrapper
class.
这篇关于安卓:为什么一定要用getBaseContext()而不是此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!