我必须为我的 Android 应用程序创建一个“multimap”类型的数据结构,如下所示
HashMap<Integer, String []> sampleStorage = new HashMap<Integer, String []>
我试图将其传递到多个 Activity ,但它无法传递到第一个 Activity 。我知道这一点是因为数据在创建它的 Activity 中可用,但在我传递 Intent 的 Activity 中不可用。
我正在使用此代码将其添加到 Intent 中并发送
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");
当我尝试在第二个 Activity 中访问它时引发的错误是 NullPointerError,因此看起来它甚至没有进入第二个 Activity 。任何帮助将不胜感激,提前谢谢您。
最佳答案
因此,经过几天的代码搜索,我发现有一个 SampleStorage 的二次声明覆盖了我的初始 HashMap 。抱歉,我最初没有提供足够的代码供某人正确回答问题。
关于java - 在android studio中通过intent发送MultiMap类型数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51072955/