简体   繁体   中英

Passing HashMap using intent returns null, why?

I'm trying to pass a hashmap from one activity to another using intent. I already found a solution which should work. I implemented Serializable in class Player.

private HashMap<Integer, Player> players = new HashMap<Integer, Player>();
players.put(1, new Player("name"));
Intent intent = new Intent(this, GameActivity.class);
intent.putExtra("players", players);
startActivity(intent);

But when I want to get the hashmap in the other activivity it returns null.

Intent intent = getIntent();
players = (HashMap<Integer, Player>) intent.getSerializableExtra("player");

What am I doing wrong? Any suggestions? I just can't find my mistake.

I believe there is a typo here:

Intent intent = getIntent();
players = (HashMap<Integer, Player>) intent.getSerializableExtra("player");

Did you mean:

getSerializableExtra("players");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM