我正在尝试将Hashmap传递给另一个意图,但我收到错误:
Cannot resolve method 'putExtra(java.lang.String,java.util.Map<String,android.content.pm.ApplicationInfo>
码:
Map<String, ApplicationInfo> map = returnedMap;
Intent i = new Intent(LoadingScreen.this, DisplayClass.class);
i.putExtra("total",total);
i.putExtra("map", map);
startActivity(i);
Intent.putExtra
只接受原始类型/ Parcelable
,Serializable
对象。
因为你持有Map
,所以很容易将它作为HashMap
发送,因为HashMap
实现了Serializable
接口。
// If your originial Map is not a HashMap, you have to convert it
// HashMap<String, ApplicationInfo> map = new HashMap<>(returnedMap);
// If it's already an HashMap, you have to cast it
// HashMap<String, ApplicationInfo> map = (HashMap)returnedMap;
i.putExtra("map", map);
在接收方:
HashMap<String, ApplicationInfo> map = (HashMap<>)intent.getSerializableExtra("map")
你需要创建一个HashMap
原因HashMap
是实现Serializable
的类。 Map
是interface
的父母HashMap
。
HashMap<String, ApplicationInfo> map = returnedMap;
Intent i = new Intent(LoadingScreen.this, DisplayClass.class);
i.putExtra("total",total);
i.putExtra("map", map);
startActivity(i);
由于HashMaps序列化而地图没有,因此请尝试执行以下段,因为您正在使用
Intent.putExtra(String,Serializable)方法:
HashMap<String, ApplicationInfo> hashMap = returnedMap;