android.util.AndroidRuntimeException:

简介: 引用:http://hi.baidu.com/jlcust/blog/item/8cb8b61341807fd9a7ef3f92.html   Caused by: android.util.

引用:http://hi.baidu.com/jlcust/blog/item/8cb8b61341807fd9a7ef3f92.html

 

Caused by: 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); 

以上转自:http://www.cnblogs.com/rioder/archive/2011/11/02/2233584.html

以下为Context源码中的startActivity:

    /**
     * Launch a new activity.  You will not receive any information about when
     * the activity exits.
     *
     * <p>Note that if this method is being called from outside of an
     * {@link android.app.Activity} Context, then the Intent must include
     * the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag.  This is because,
     * without being started from an existing Activity, there is no existing
     * task in which to place the new activity and thus it needs to be placed
     * in its own separate task.
     *
     * <p>This method throws {@link ActivityNotFoundException}
     * if there was no Activity found to run the given Intent.
     *
     * @param intent The description of the activity to start.
     *
     * @throws ActivityNotFoundException
     *
     * @see PackageManager#resolveActivity
     */
    public abstract void startActivity(Intent intent);

以上解释了为什么使用Context的startActivity方法(比如在Service中或者BroadcastReceiver中启动Activity)为什么需要添加flag:FLAG_ACTIVITY_NEW_TASK

相关文章
|
7月前
|
JSON API 数据格式
阿里巴巴商品详情接口(阿里巴巴 API 系列)
在电商开发中,获取阿里巴巴商品详情信息对数据分析、竞品研究等至关重要。通过调用其商品详情接口,开发者可获取标题、价格、图片、描述等数据,满足多种业务需求。接口采用HTTPS协议,支持GET/POST请求,返回JSON格式数据。示例代码展示了如何使用Python的requests库进行接口请求,需传递商品ID和访问令牌。实际应用时,请依据官方文档调整参数并确保安全性。
240 10
|
Android开发 UED Kotlin
kotlin webview 加载网页失败后如何再次重试
在Kotlin中,当使用WebView加载网页失败时,可通过设置WebViewClient并覆盖`onReceivedError`方法来捕获失败事件。在该回调中,可以显示错误信息或尝试使用`reload()`重试加载。以下是一个简要示例展示如何处理加载失败
|
安全 Android开发 C++
在 Android 中使用 Kotlin 调用动态库
在 Android 中使用 Kotlin 调用动态库
623 0
|
Android开发
Android studio 出现Plugin [id: ‘com.android.application‘, version: ‘8.1.0‘, apply: false] 问题解决办法
Android studio 出现Plugin [id: ‘com.android.application‘, version: ‘8.1.0‘, apply: false] 问题解决办法
5425 2
|
人工智能 安全 Devops
让研发规范管得住,在流水线之上做研发流程
研发规范的目标,是为了解决或降低出现软件危机的风险。但传统流水线受限于工具的定位,无法解决研发规范的落地问题,需要在更高的层面来解决。阿里云云效团队经过内部启发后推出的新产品:云效应用交付平台 AppStack 给出了解决方案,快来使用体验吧!
79681 7
|
安全 API Android开发
Android应用开发中实现动态权限请求的实践指南
【4月更文挑战第5天】 在现代安卓应用开发中,用户隐私和安全日益受到重视。自Android 6.0(API级别23)起,系统引入了动态权限模型,要求应用在运行时而非安装时请求特定权限。这一变化迫使开发者调整权限请求策略,以增强用户信任并确保应用的正常运行。本文将深入探讨如何在Android应用中有效实施动态权限请求,提供一套详细的实践流程,并通过代码示例阐释关键步骤。我们将讨论如何判断权限的必要性、向用户发起请求的最佳时机以及如何处理用户的授权结果。
|
XML 算法 Java
Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)
Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)
778 0
|
开发工具 Android开发 iOS开发
如何使用 Draggable 和 DragTarget 在 Flutter 中创建拖放 UI 元素?
如何使用 Draggable 和 DragTarget 在 Flutter 中创建拖放 UI 元素?
657 0