原代码如下:

Intent intent = new Intent();
intent.setClass(mContext, PhotoView.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);
mContext.startActivity(intent);

报错如下:

06-28 11:24:40.359: E/AndroidRuntime(7397): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
06-28 11:24:40.359: E/AndroidRuntime(7397): at android.app.ContextImpl.startActivity(ContextImpl.java:617)
06-28 11:24:40.359: E/AndroidRuntime(7397): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
06-28 11:24:40.359: E/AndroidRuntime(7397): at com.hotalk.ui.homepage.favorites.FavoritesListAdapter$7.onClick(FavoritesListAdapter.java:1509)
06-28 11:24:40.359: E/AndroidRuntime(7397): at android.view.View.performClick(View.java:2408)
06-28 11:24:40.359: E/AndroidRuntime(7397): at android.view.View$PerformClick.run(View.java:8816)
06-28 11:24:40.359: E/AndroidRuntime(7397): at android.os.Handler.handleCallback(Handler.java:587)
06-28 11:24:40.359: E/AndroidRuntime(7397): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 11:24:40.359: E/AndroidRuntime(7397): at android.os.Looper.loop(Looper.java:123)
06-28 11:24:40.359: E/AndroidRuntime(7397): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 11:24:40.359: E/AndroidRuntime(7397): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 11:24:40.359: E/AndroidRuntime(7397): at java.lang.reflect.Method.invoke(Method.java:521)
06-28 11:24:40.359: E/AndroidRuntime(7397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 11:24:40.359: E/AndroidRuntime(7397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 11:24:40.359: E/AndroidRuntime(7397): at dalvik.system.NativeStart.main(Native Method)

原因是:

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

修改后代码如下:

Intent intent = new Intent();
intent.setClass(mContext, PhotoView.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);

startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag的更多相关文章

  1. startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI

    原代码如下: Intent intent = new Intent(); intent.setClass(mContext, PhotoView.class); Bundle bundle = new ...

  2. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

    Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK fla ...

  3. android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? com.uethinking.microvideo.manag

    记录学习,网络摘抄 碰到这个异常其实这个上面说的很清楚,加个flag 从一个Activity中要通过intent调出另一个Activity的话,需要使用 FLAG_ACTIVITY_NEW_TASK ...

  4. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

    https://blog.csdn.net/watermusicyes/article/details/44963773 Context中有一个startActivity方法,Activity继承自C ...

  5. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

    安卓出现如下错误,需要增加FLAG_ACTIVITY_NEW_TASK标志 Intent intent1 = new Intent(getApplicationContext(), CameraAct ...

  6. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fla

      转载于 http://blog.csdn.net/wike163/article/details/6678073    从一个Activity中要通过intent调出另一个Activity的话,需 ...

  7. 兔子--Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK

    错误原因: Activity打开别的Activity的时候会默认把新的Activity放到自己的task中,所以不用指定,可是别的地方就得指定了. 解决的方法:intent.addFlags(Inte ...

  8. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW _TASK flag.

    在Activity中使用startActivity()方法不会有任何限制,因为Activity重载了Context的startActivity()方法.但是如果是在其他地方(如Widget或Servi ...

  9. 使用eclipse启动tomcat里的项目时报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    1.这种错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener刚开始看的时候 ...

随机推荐

  1. Gym - 100203I I WIN 网络流

    Gym - 100203I  I WIN 题意:一个n*m的矩阵包含W,I,N三种字符,问相邻的字符最多能组成不重叠的WIN. 思路:比赛的时候没有发现是网络流,,居然一度以为是二分图匹配,,写了一下 ...

  2. Ionic2集成ngx-datatable,ng2-tree第三方控件.md

    1. 基本环境配置 1.1. 命令安装相应的依赖 1.2. 在Module定义中引入对应Module 1.3. 引入对应的CSS 2. 简单使用示例验证是否集成成功 2.1. ngx-datatabl ...

  3. 16个ASP.NET MVC扩展点【附源码】

    转载于:http://www.cnblogs.com/wupeiqi/p/3570445.html 1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中! ...

  4. Linq中where查询

    Linq的Where操作包括3种形式:简单形式.关系条件形式.First()形式. 1.简单形式: 例:使用where查询在北京的客户 var q = from c in db.Customers   ...

  5. css3 列表图片hover左右滚动效果

  6. BFS(广度优先搜索)

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  7. vue中eventbus的使用

    eventbus的方法很是简单,我们需要做三步事情: 第一步,我们需要创造一个容器去充当我们的eventbus 第二步,我们需要去抛出,或者说提交我们的事件 第三步,我们去监听我们的那个事件(也许这才 ...

  8. MYSQL常用命令列表

    MYSQL常用命令列表 1.系统管理 mysql -h主机地址 -u用户名 -p 连接MYSQL(在mysql/bin) exit 退出MYSQL命令 mysqladmin -u用户名 -p旧密码 p ...

  9. centos yum 安装php7.2

    yum -y remove php* rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm r ...

  10. Ubuntu安装php

    http://jingyan.baidu.com/article/dca1fa6fadc61ff1a5405244.html