startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
The of from an New Activity Task Context
2023-09-11 14:17:58 时间
原代码如下:
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);
相关文章
- Twitter OA prepare: Visit element of the array
- Visual Studio Code is not showing the ouput of Python
- fatal: The upstream branch of your current branch does not match the name of your current branch
- The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
- The property 'RowId' is part of the object's key information and cannot be modified.
- The 16th tip of DB Query Analyzer
- English trip -- VC(情景课)4 B Parts of the body 身体部位
- CDOJ 1220 The Battle of Guandu
- Unable to load the mojo 'resources' (or one of its required components) from the plugin......
- HDU 4461 The Power of Xiangqi (水题)
- 继承“HibernateDaoSupport”后,报“The hierarchy of the type AccoutDaoImpl is inconsistent”的解决方案
- 【解决方案】ERROR: lib/bridge_generated.dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
- Eclipse启动 报错[Failed to load the JNI shared library jvm.dll
- Change the color of a link in an NSMutableAttributedString
- The Future Of ReactiveCocoa by Justin Spahr-Summers
- MYSQL导入csv类型的数据出现The MySQL server is running with the --secure-file-priv option
- Maven报错 解决方案。ERROR: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id
- 项目重新部署后报The attribute required is undefined for the annotation type XmlElementRef
- Linux C/C++ ------ “” and <> in the use of head include file(Pending Verification)
- 【RefactoringCode】The description of the refactoring book
- HDU 1010 Tempter of the Bone heuristic 修剪
- [LeetCode] 1281. Subtract the Product and Sum of Digits of an Integer 整数的各位积和之差