安卓:为什么一定要用getBaseContext()而不是此 [英] Android: why must use getBaseContext() instead of this

查看:944
本文介绍了安卓:为什么一定要用getBaseContext()而不是此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经常引用当前上下文。但是,在某些情况下,我们为什么一定要用 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()将收到错误。

谁能给我解释一下,谢谢。

解决方案
  1. getApplicationContext()返回整个应用程序生命周期的应用程序环境中,当应用程序会破坏那么它也会破坏。

  2. 的背景下返回活动的当前背景下,属于活动,该活动被破坏,然后它会破坏also.but在你的情况下,意志是指微调实例,因为我们是在 onItemSelected使用这个(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3)方法,这是由微调类和微调继承 AdapterView.OnItemSelectedListener 接口

  3. getBaseContext() ContextWrapper 的方法。而 ContextWrapper 进行代理执行上下文的,仅仅代表其所有调用另一个上下文。可以被子类修改行为,而不改变原有的语境。(按的javadoc)。

和你的情况:微调类不是子背景 ContextWrapper 类*

  Toast.makeText(getBaseContext(),选中,Toast.LENGTH_SHORT).show();
 

办法 getBaseContext() ContextWrapper ContextWrapper 是通过代理执行上下文所以间接地我们传递一个上下文类对象。

或者我们也可以通过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.

解决方案

  1. getApplicationContext () returns the application context of the entire application life cycle,when application will destroy then it will destroy also.

  2. 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 the Spinner instance because we are using this within onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3) method which is from Spinner class and Spinner inherit this method from AdapterView.OnItemSelectedListener interface

  3. getBaseContext() is the method of ContextWrapper. And ContextWrapper 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屋!

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