300_不能使用ApplicationContext
来源:互联网 发布:淘宝卖家最迟发货时间 编辑:程序博客网 时间:2024/05/16 19:50
不能使用ApplicationContext
有些情况不能使用ApplicationContext
1.创建展示dialog
Dialog dialog = new Dialog(getApplicationContext());
dialog.show();
报错:
android.view.WindowManager$BadTokenException:
Unable to add window -- token null is not for an application
2.启动activity
getApplicationContext().startActivity(new Intent(this, NewActivity.class));
报错:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
要这样:
Intent intent = new Intent(this, NewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
但是不推荐这样做
3.使用LayoutInflater或者调用inflate()
View view = View.inflate(getApplicationContext(), R.layout.view, null);
或者
View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view, null);
这两种使用applicationContext是合法的
但是会使用系统默认的主题样式,如果自定义了样式有可能失效
0 0
- 300_不能使用ApplicationContext
- 7.4.2:使用ApplicationContext
- ApplicationContext.xml数据源使用,
- 使用ApplicationContextAware得到ApplicationContext
- spring ApplicationContext 使用总结
- spring中使用ApplicationContext
- applicationContext中事物的使用
- ApplicationContext 的使用时机 - [Spring]
- Spring 中ApplicationContext使用 <02>
- 使用的bean工厂ApplicationContext
- applicationContext使用配置文件的值后报错
- ApplicationContext
- ApplicationContext
- ApplicationContext
- ApplicationContext
- ApplicationContext
- ApplicationContext
- ApplicationContext
- HTML5学习_day13(3)--border-radius属性(圆角边框)
- Babelfish Poj2503(STL map的使用)
- mysql存储过程详解
- 使用cv::findFundamentalMat要注意的几点
- 3-11 LED驱动程序设计
- 300_不能使用ApplicationContext
- poj2229Sumsets递归/动态规划
- javaweb项目中导入jquery
- IDEA启动tomcat失败
- win7环境下eclipse gdb调试输出stl容器内容
- mongo date 类型
- 没有重复字符的最长子字符串(Longest Substring Without Repeating Characters)
- 301_SerialExecutor解析
- PE结构详细图解