原代码如下:

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_ACTIVI的更多相关文章

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

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

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

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

  3. 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的话,需 ...

  4. 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 ...

  5. 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 ...

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

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

  7. 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 ...

  8. 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 ...

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

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

随机推荐

  1. sql server 2008 身份验证失败 18456

    双击打开后加上  ;-m  然后以管理员方式  打开 SQLSERVER 2008  就可以已window身份登录  不过还没有完 右键  属性  =>安全性 更改为 sql server 和 ...

  2. 重置AD用户密码

    $cc = import-csv D:\Powershell\Tauba.csv foreach ($c in $cc) { $c.username $pwd = $c.password get-ad ...

  3. cisco ASA ios升级或恢复

    cisco ASA ios升级或恢复 一.升级前准备工作 1.准备好所要升级的IOS文件及对应的ASDM文件 2.在一台电脑上架设好tftp,设置好目录,与防火墙进行连接(假设电脑IP为192.168 ...

  4. python基础学习14----正则表达式

    正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 在python中正则表达式被封 ...

  5. 50家硅谷IT公司技术博客

    分享一下 50 家硅谷优秀 IT 公司技术博客,从中可以了解企业文化,技术特色和设计语言,如果直接列出来很单调,加上点评,算吐槽版吧. 知名大厂   1. Facebook https://www.f ...

  6. sql点滴45—mysql中group_concat用法

    group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果.比较抽象,难以理解. 通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组, ...

  7. 【8】python文件的读写方法

    (1).读文件的步骤: (1)打开文件 open(path,flag,encoding,[errors]) path:打开路径 flag:打开方式 r(只读) rb(二进制格式) r+(可以读写) w ...

  8. NoSQL——not onlySQL不仅仅是SQL

    数据有很大一部分是由关系数据库管理系统(RDBMS)来处理. 1970年 E.F.Codd's提出的关系模型的论文 "A relational model of data for large ...

  9. 根据进程ID查找运行程序目录

    查看进程ID [root@hadoop03 openresty]# netstat -nltp 进入/proc目录查找相应进程ID目录并进入此目录 [root@hadoop03 usr]# cd /p ...

  10. Oracle 11g AWR 系列五:如何生成 AWR 报告?

    1.生成单实例 AWR 报告: @$ORACLE_HOME/rdbms/admin/awrrpt.sql 2.生成 Oracle RAC AWR 报告: @$ORACLE_HOME/rdbms/adm ...