Android使用非Activity引用启动页面报错!
来源:互联网 发布:06款雅马哈r1数据 编辑:程序博客网 时间:2024/06/06 03:50
作者:燕歆波
导读:我从来没碰到过这个问题,直到朋友碰到了告诉我,他说如果使用非activity的引用来启动activity,那么,系统会报出异常,我们当时在手机上也试了几次,可是竟然没出错?当时,我们并没有去百度这个问题。。。
问题
在启动页面时,我们经常使用当前页面的引用去开启新的页面,可是如果不使用页面的引用,会怎么样呢,我们使用application的引用试一下就知道了:
ECardApplication context = ECardApplication.getInstance(); Intent intent = new Intent(this, MySettingActivity.class); context.startActivity(intent);
运行后,点击按钮执行上面的代码,直接就报错了:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
错误中还给出了解决方法:
使用非activity引用启动页面,需要添加FLAG_ACTIVITY_NEW_TASK flag
既然这样,我们就按照他说的给intent添加一个flag,然后再试一次。
结果成功了!
突然想到,在使用自己的手机测试的时候,并没有出错,可是这里为什么出错了呢,于是我去掉flag,在自己手机上又测试了一次,还是没有出错,我和朋友当时很郁闷,为什么没有错误了呢,当时猜想是手机7.0的问题。
周一上班,朋友突然对我说,他发现了原因,的确是7.0的问题,原因呢,让我看一下ContextImpl类,这个类并不是公开的,于是我在sdk下找到了这个类:
@Override public void startActivity(Intent intent, Bundle options) { warnIfCallingFromSystemProcess(); // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is // generally not allowed, except if the caller specifies the task id the activity should // be launched in.if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0 && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) { throw new AndroidRuntimeException( "Calling startActivity() from outside of an Activity " + " context requires the FLAG_ACTIVITY_NEW_TASK flag." + " Is this really what you want?"); } mMainThread.getInstrumentation().execStartActivity( getOuterContext(), mMainThread.getApplicationThread(), null, (Activity) null, intent, -1, options); }
看了7.0的,再看6.0的,我们就会发现问题:
@Override public void startActivity(Intent intent, Bundle options) { warnIfCallingFromSystemProcess(); if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { throw new AndroidRuntimeException( "Calling startActivity() from outside of an Activity " + " context requires the FLAG_ACTIVITY_NEW_TASK flag." + " Is this really what you want?"); } mMainThread.getInstrumentation().execStartActivity( getOuterContext(), mMainThread.getApplicationThread(), null, (Activity) null, intent, -1, options); }
发现什么不同 的地方吗,在6.0
if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0)
和7.0的
if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
&& options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)
明显多了一些条件,
总结:6.0在启动页面之前会检查有没有Intent.FLAG_ACTIVITY_NEW_TASK,没有设置的话就报上面所说的那个异常,而7.0除此之外需要options不为null,还有启动的任务id不为-1,才会成立报出异常,
- Android使用非Activity引用启动页面报错!
- Android 启动activity 空白页面
- Android 代码启动activity页面
- 关于Android使用隐式启动Activity报No Activity found to handle Intent的问题
- 使用maven启动项目jsp页面报错
- Android 启动Activity使用ActivityForResult
- Android:XListView使用AS默认继承的Activity报错
- 在非Activity类里面启动Intent、Toast等、非Activity类引用getResources()方法问题的解决方法
- android在非Activity中使用startActivity
- Android 报错 Activity 状态
- android在非Activity下,intent页面跳转问题
- Linux 中Tomcat 服务器 启动项目,页面删除报错“该信息被其他信息引用”解决 (ORA-02292)
- Tomcat在非Root用户下使用80端口启动报错解决办法
- 使用ApplicationContext启动standard模式的Activity报错原因解析
- Android Activity---启动Activity
- Android Activity---启动Activity
- Android使用两个Activity页面切换…
- Android使用两个Activity页面切换…
- 从Docker Hub上下载Image并修改Image的Repository属性
- PHP的list
- 纯js 实现弹幕效果
- httpsession跨域请求不能保存的问题
- 基于bs4的HTML内容遍历方法
- Android使用非Activity引用启动页面报错!
- 在IDEA下 spring+springMVC+hibernate+maven框架搭建
- 【HDU
- Android 获取联系人和电话号码
- Java curator操作zookeeper获取kafka
- c#字符常用方法
- 使用eclipse启动系统时报错“ java.lang.OutOfMemoryError: PermGen space”问题的解决
- bootstrap 清楚浮动
- 23种设计模式-前言