android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity_android android.util.androidruntimeexception: call-程序员宅基地

技术标签: Android  

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用 Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。解决办法是,加一个flag。

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

以上转自:http://www.cnblogs.com/rioder/archive/2011/11/02/2233584.html

以下为Context源码中的startActivity:

    /**
     * Launch a new activity.  You will not receive any information about when
     * the activity exits.
     *
     * <p>Note that if this method is being called from outside of an
     * {@link android.app.Activity} Context, then the Intent must include
     * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag.  This is because,
     * without being started from an existing Activity, there is no existing
     * task in which to place the new activity and thus it needs to be placed
     * in its own separate task.
     *
     * <p>This method throws {@link ActivityNotFoundException}
     * if there was no Activity found to run the given Intent.
     *
     * @param intent The description of the activity to start.
     *
     * @throws ActivityNotFoundException
     *
     * @see PackageManager#resolveActivity
     */
    public abstract void startActivity(Intent intent);

以上解释了为什么使用Context的startActivity方法(比如在Service中或者BroadcastReceiver中启动Activity)为什么需要添加flag:FLAG_ACTIVITY_NEW_TASK



转:http://hi.baidu.com/jlcust/item/7074c096a534f04df042154d

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/WALLEZhe/article/details/50757582

智能推荐

编写一段网页文字放大动画-程序员宅基地

文章浏览阅读70次。如果要在网页上实现文字放大动画,可以使用 CSS 动画或 JavaScript 动画。使用 CSS 动画,可以通过设置 transition 属性来实现动画效果,例如:.text { transition: transform 0.5s; /* 设置动画时间为 0.5 秒 */}.text:hover { transform: scale(1.2); /* 鼠标悬浮时文字放大..._css 动画设置字体变大动画

在fluent计算时如何设置非稳态时的时间步长_fluent时间步长设置-程序员宅基地

文章浏览阅读1.8w次,点赞2次,收藏12次。1、先测出沿流向最小的网格长度2、用该网格长度除以你的进口速度,这就是非稳态的时间步长,能够较好的表现流动特征,特别在计算升力、阻力系数时,能够使你的数据与实验对比较好!_fluent时间步长设置

Linux常用命令——su命令_linux su命令-程序员宅基地

文章浏览阅读819次。用于切换当前用户身份到其他用户身份,变更时须输入所要变更的用户帐号与密码。用于切换当前用户身份到其他用户身份。用户:指定要切换身份的目标用户。变更帐号为root并传入。su(选项)(参数)_linux su命令

ubuntu 编译 jdk (三)_langtools: langtools-only-程序员宅基地

文章浏览阅读2.1k次。 root@ubuntu:/home/gap/jdk8u# make WARNING: You have the following ALT_ variables set:ALT_OUTPUTDIR=/home/gap/buildALT_ variables are deprecated and will be ignored. Please clean your envi..._langtools: langtools-only

yolov8超详细从配置环境到训练测试_yolov8环境配置-程序员宅基地

文章浏览阅读9.4k次,点赞13次,收藏120次。超详细,半天让你使用好yolov8代码_yolov8环境配置

oracle sql经典面试题,经典SQL面试题2-程序员宅基地

文章浏览阅读296次。题目:一张名为workersalary的表,要求查询出全部信息,并且salary最高的三个人按升序排列在结果的最开头,其余的人按原有顺序排列。这个sql如何写?解答:(1)题意理解假如原先的表是这样的namesalaryLiuYi2000ChenEr1000ZhangSan5000LiSi4000WangWu8000ZhaoLiu6000SunQi7000ZhouBa3000题目要求变成这样nam..._sql面试必会6题经典oracle必问的面试题

随便推点

vue页面动态切换_vue 首页 切换动态画面-程序员宅基地

文章浏览阅读2k次。Vue-router结合transition实现app前进后退动画切换效果首先,配置路由并且修改路由配置配置路由的重点是给Router添加一个goBack方法,用于记录路由的前进状态 this.isBack = trueRouter.prototype.goBack = function () {   this.isBack = true  window.history.go(-1..._vue 首页 切换动态画面

Jupyter NoteBook 中使用 cv2.imshow 显示图片_jupyter cv2.imshow-程序员宅基地

文章浏览阅读3.9w次,点赞26次,收藏32次。Jupyter NoteBook 中使用 cv2.imshow 显示图片有两种办法:用 cv2.imshow时加入cv2.destroyAllWindows()用 plt.imshow() 代替 cv2.imshow1. cv2.imshow加入 cv2.destroyAllWindows() 后可以解决 crash 或者图片显示不出来的问题。import cv2%matplot..._jupyter cv2.imshow

android webView使用-程序员宅基地

文章浏览阅读40次。在Android手机中内置了一款高性能webkit内核浏览器,在SDK中封装为一个叫做WebView组件。一、使用WebView1.加入权限 <uses-permission android:name="android.permission.INTERNET" />2.创建一个webView通过布局 <WebView ...

怎么保存python制作的饼图_Scribus中的Python脚本:制作饼图-程序员宅基地

文章浏览阅读581次。Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发。Scribus是一个强大的开源页面布局应用程序,它是由开源社区自2001年开发的程序。据项目网站可知,Scribus是为Linux,FreeBSD,PC-BSD,NetBSD,OpenBSD,Solaris和程序提供 CMYK颜色,专色,ICC颜色管理和PDF创建的程序。该网站还支持应用程序的高级功能,如矢..._python绘制饼图保存本地

计算机科学与导论期末考,《计算机科学导论》期末考试试题-程序员宅基地

文章浏览阅读475次。《计算机科学导论》期末考试试题(B卷)班级:姓名:学号:成绩:一、单项选择题1. 用一个字节表示无符号整数,其最大值是十进制数()。A. 256B. 255C. 127D. 1282. 一个完整的计算机系统应包括()。A. 运算器、控制器和存储器B. 主机和应用程序C. 硬件系统和软件系统D. 主机和外部设备3. 微机中的CPU是指()。A. 内存和运算器B. 输入设备和输出设备C. 存储器和控..._计算机科学导论期末题

Mybatis配置之properties和settings标签_<configuration> <properties resource="... /> <sett-程序员宅基地

文章浏览阅读1.8k次,点赞3次,收藏3次。本次将讲述mybatis的配置,主要的配置是基于mybatis的配置文件来进行配置的,其配置文件并不复杂,如下面的例子: _

推荐文章

热门文章

相关标签