startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
来源:互联网 发布:高中封闭式管理 知乎 编辑:程序博客网 时间:2024/05/23 19:31
原代码如下:
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);
阅读全文
0 0
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIV
- Context启动startActivity时报错:Calling startActivity() from outside of an Activity
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fla
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- Calling startActivity() from outside of an Activity context requires the FLA
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fl
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fla
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
- AndroidRuntimeException “Calling startActivity() from outside of an Activity context requires the FL
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK fla
- AbstractQueuedSynchronizer源码剖析(四)- 不响应中断的共享锁
- 微信小程序----组件之text
- HDU6186CS Course前缀和
- html5播放器的示例代码
- 如何在linux下开启FTP服务
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI
- [机器学习实验4]正则化(引入惩罚因子)
- 《重构-改善既有代码的设计》知识精选——代码坏味道
- 2017智慧树 创践大学生创新创业实务答案
- Wdatepicker日期控件的使用指南
- opencv 矩阵元素排序(代码)
- java NIO关于*Buffer的 flip | reset | rewind | clear 的灵活使用备忘
- 面向对象三大特征之继承详解
- 汇编函数封装 pc寄存器 符号标签 数字标签