You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 11中Bundle.getParcelableArrayList方法报错问题求助

问题原因

你调用的Bundle.getParcelableArrayList(String, Class<T>)是**Android 13(API Level 33)**才新增的系统方法,Android 11(API Level 30)的系统框架中不存在这个方法,因此运行时会抛出NoSuchMethodError。

解决方案

有两种可靠的修复方式,根据项目情况选择:

方案一:使用旧版兼容方法

直接替换为从API 1就存在的getParcelableArrayList(String)方法,该方法兼容所有Android版本,仅因类型安全问题被标记为过时,可通过注解忽略警告:

val bundle = intent.extras
@Suppress("DEPRECATION")
listOfPost = bundle?.getParcelableArrayList<PostsList>("PostData") as ArrayList<PostsList>

若要避免强转可能引发的崩溃,可优化为:

val bundle = intent.extras
@Suppress("DEPRECATION")
listOfPost = bundle?.getParcelableArrayList<PostsList>("PostData") ?: arrayListOf()

方案二:使用AndroidX兼容工具类(推荐)

确保项目已依赖AndroidX Core库(新项目默认集成),使用BundleCompat提供的跨版本兼容方法,无需处理过时警告,同时保证类型安全:

import androidx.core.os.BundleCompat

val bundle = intent.extras
listOfPost = BundleCompat.getParcelableArrayList(bundle, "PostData", PostsList::class.java) ?: arrayListOf()

该方法内部会自动适配系统版本:在Android 13+使用新API,低版本系统则调用旧版兼容逻辑。

内容的提问来源于stack exchange,提问作者MARSH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 14:50:10