startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIV
2016-06-17 22:13
435 查看
原代码:
Intent intent = new Intent(); intent.setClass(mContext, PhotoView.class); Bundle bundle = new Bundle(); intent.putExtras(bundle); mContext.startActivity(intent);
所报错误:
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?原因:
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);
相关文章推荐
- 2541添加128bitUUID
- Setup SSH access between VirtualBox Host and Guest VMs
- 1065 - Number Sequence &&1070 - Algebraic Problem
- 334. Increasing Triplet Subsequence
- 300 Longest Increasing Subsequence
- Leetcode 62. Unique Paths
- 如何修改UIButton按下后默认的蓝色效果
- IOC_@ImportResource@Value
- No 'Access-Control-Allow-Origin' header is present on the requested resource
- 圆角按钮--继承自JButton
- UE4登录长时间等待问题
- FDQuery 怎么能插入NULL参数
- 10位以内二进制数字转十进制计算器(Printing the Decimal Equivalent of a Binary Number)
- UE4(二)学习之路之Matinee制作屏幕淡入淡出效果
- Android学习之 图解调用invalidate()和requestLayout()的过程
- UIAutomator remote debug方法
- easyui datagrid
- UICollectionView基础
- iOS开发UI篇—控制器的View的创建
- ORA-22922 :nonexistent LOB value解决方案