Q:This
The current context is often referenced. HoweverGetbasecontext ()
To replaceThis
. That is to say, useThis
An error is thrown.
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 }
WhenGetbasecontext ()
ChangeThis
There will be errors.
Why must I useGetbasecontext ()
Method, but cannot useThis
What about it?
A:
1.Getapplicationcontext ()
Method returns the context of the entire application lifecycle when the application is to be destroyed.
2.This
The application context returns the current context of the activity, which belongs to the activity. When it is destroyed, the activity is also destroyed. But in your case, it refers to the spinner instance, because weNitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3)
Method. This method is from the spinner class, whileAdapterview. onitemselectedlistener
Interface to inherit this method.
3.Getbasecontext ()
YesContextwrapper
.
Reference: http://ask.csdn.net/questions/942
Why use the getbasecontext () method instead of this? (Transfer)