首页 > 解决方案 > 通过 android studio 中的意图发送 MultiMap 类型的数据结构

问题描述

我必须为我的 android 应用程序创建一个“multimap”类型的数据结构,看起来像这样

HashMap<Integer, String []> sampleStorage = new HashMap<Integer, String []>

我试图通过几个活动传递它,但它未能传递给第一个活动。我知道这一点是因为数据在它创建的活动中可用,但在我将意图传递给的活动中不可用。

我正在使用此代码将其添加到意图并发送它

Intent intent = new Intent(this, MyActivity.class);

Bundle args = new Bundle();
args.putSerializable("sampleStorage", (Serializable)sampleStorage);
intent.putExtra("BUNDLE", args);

这是我用来检索它的代码

Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");

sampleStorage = (HashMap<Integer, String []>) args.getSerializable("sampleStorage");

当我尝试在第二个活动中访问它时引发的错误是 NullPointerError 所以它似乎甚至没有进入第二个活动。任何帮助将不胜感激,在此先感谢您。

标签: javaandroidandroid-studioandroid-intentandroid-bundle

解决方案


因此,经过几天的代码搜索,我发现有一个 sampleStorage 的辅助声明覆盖了我的初始哈希映射。抱歉,我最初没有提供足够的代码让某人首先正确回答问题。


推荐阅读