Context ——常见问题
来源:互联网 发布:java线程sleep函数 编辑:程序博客网 时间:2024/05/18 02:18
一、为什么要用getBaseContext()方法代替this
this
常常引用当前的 context。但是有些时候,必须使用getBaseContext()
来代替this
。就是说使用this
会引发错误。
如下面的例子:
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
就会有错误。为什么这种情况下必须使用
getBaseContext()
方法,而不能使用this
呢?答案:
二、new AlertDialog.Builder(getApplicationContext())时会发生错误
答案:AletrDialog依赖的是一个View, 而View对应一个Activity,若传入getApplicationContext(),其生命周期是整个应用,当退出当前Activity的时候,就会报Unable to add window -- token null is not for an application的错误,应该传入当前Activity的Context。
0 0
- Context ——常见问题
- Context——Context详解
- Context——Android中Context简介
- Context——Context详解(2)
- Context——Context的使用
- Context——Context详解(6)
- Esper——Context
- Context——Context的使用(2)
- Context——Context的详解(4)
- Context——Context的详解(5)
- TQ210——常见问题
- Tomcat配置——context
- Android——Context.getString()
- Android——Context类
- Android——context理解
- SSH常见问题集锦—
- SpringMVC常见问题java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- jbpm4——常见问题解答
- Eclipse 项目有红感叹号
- CUDA学习(二)上古时期的武器装备
- WinSCP实现windows和Linux大文件的传输
- ubuntu12.04无法使用apt-get install问题
- android 获取 网络科普
- Context ——常见问题
- test·A·Summary
- 自识别标记(self-identifying marker) -(3) 用于相机标定的CALTag源码剖析(上)
- 平面列表
- C语言深度剖析-读书简记
- android 监听键盘事件,搜索,确定
- 什么是Spring IOC
- 常用的jQuery引用地址
- 【R语言或PYTHON语言入门必读】大数据分析师到底需要懂什么(二)