原代码如下:

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. DEDE 修改后台图集上传单个图片的大小限制

    默认情况下,DEDE图集中单个图片大小限制在2M以内,而有时我们需要上传一个2M以上的文件,这是只要修改几个文件就可以实现了. 一.需要修改php.ini这个文件,我们必须保证PHP的配置中允许上传一 ...

  2. 23种JavaScript设计模式

    原文链接:https://boostlog.io/@sonuton/23-javascript-design-patterns-5adb006847018500491f3f7f 转自: https:/ ...

  3. BZOJ3645: Maze(FFT多项式快速幂)

    Description 众维拉先后在中土大陆上创造了精灵.人类以及矮人,其中矮人是生性喜好常年居住在地下的洞穴的存在,他们挖掘矿物甚至宝石,甚至用他们的勤劳勇敢智慧在地底下创造出了辉煌宏大的宫殿,错综 ...

  4. 参考《深度学习原理与应用实践》中文PDF

    读国内关于深度学习的书籍,可以看看<深度学习原理与应用实践>,对深度学习原理的介绍比较简略(第3.4章共18页).只介绍了"神经网络"和"卷积神经网络&quo ...

  5. shell脚本的if判断语句

    if条件判断语句 if (表达式) #if ( Variable in Array ) 语句1 else 语句2 fi 1.测试数字大小 #!/bin/sh NUM=100 if (( $NUM &g ...

  6. Scrapy框架使用代理

    使用代理抓取https://www.baidu.com/s?wd=ip # -*- coding: utf-8 -*- ''' 一.在settings.py中开启中间件 DOWNLOADER_MIDD ...

  7. 解决MyEclipse中安装或升级ADT之后SDK Target无法显示的问题

        故障现象,在MyEclipse里面安装完最新的android sdk和ADT之后,无法新建项目,Build Target为空,显示一直在loading.即如下面图里面显示的,Target Na ...

  8. POJ 1325 &amp;&amp; ZOJ 1364--Machine Schedule【二分图 &amp;&amp; 最小点覆盖数】

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13071   Accepted: 5575 ...

  9. 查看activity task相关信息

    可以使用命令 adb shell dumpsys activity 查看的结果如下 ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents ...

  10. headset-监听有线耳机插拔

    今天在做项目的时候,需要对耳机的插拔事件进行监听,所以就写了如下的一个小demo,对耳机监听事件进行验证.直接看代码 package com.example.alert; import android ...