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

问题描述

this 经常引用当前上下文.但是,在某些情况下,为什么我们必须使用 getBaseContext() 而不是 this.(这意味着使用this 时会提示错误).

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).

这是我的例子:

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
    }

在上面的代码中,当我将 getBaseContext() 更改为 this 时会收到错误.

At above code, when I change getBaseContext() to this will receive error.

谁能帮我解释一下.

推荐答案

  1. getApplicationContext() 返回整个应用程序生命周期的应用程序上下文,当应用程序销毁时,它也会销毁.

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

this 上下文返回活动的当前上下文,属于活动,活动被销毁然后它也会销毁.但在您的情况下,它将指的是 Spinner 实例,因为我们在 onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3) 方法中使用它,该方法来自 Spinnerclass 和 SpinnerAdapterView.OnItemSelectedListener 接口

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

getBaseContext()ContextWrapper 的方法.而 ContextWrapper 是, Context 的代理实现,只是将其所有调用委托给另一个 Context.可以子类化以修改行为,而无需更改原始 Context."(根据javadocs)..

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)..

在您的情况下:Spinner 类不是 ContextContextWrapper 类*

and in your case :Spinner class is not subclass of Context or ContextWrapper class*

Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show();

意味着 getBaseContext()ContextWrapper 的方法,ContextWrapperContext 的代理实现,所以我们间接地传递上下文类对象.

means getBaseContext() is method of ContextWrapper and ContextWrapper is Proxying implementation of Context so indirectly we are passing an Context Class Object.

或者我们也可以传递'Activity.this',因为Activity类是ContextWrapper类的子类.

or we can also pass 'Activity.this' because Activity class is subclass of ContextWrapper class .

如果您使用 android 文档,则此方法需要一个 Context 类对象:
public static Toast makeText (Context context, int resId, int duration)

if you go with android documention then this method require an Context class object:
public static Toast makeText (Context context, int resId, int duration)

因此我们无法将活动或类上下文意味着 this 传递给 Toast.makeText ,它们没有 ContextContextWrapper 类.

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.

这篇关于Android:为什么必须使用 getBaseContext() 而不是这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:42