我有这个纸牌游戏,我将所有玩家存储在一个List
。
为了找出我想与之合作的玩家,每个玩家都有一个Card
(我可以得到卡名),一个name
(我可以得到玩家的名字),但为了保持唯一性,每个玩家都有一个ID
.
现在,在我的开始onCreate()
方法,我找到并将列表中的玩家分配给玩家,Player
clairvoyant
:
public void initializeCharacters() {
for (Player player : players) {
if (player.getCardName().equals("Clairvoyant")) {
clairvoyant = player;
}
}
游戏在夜间和白天之间切换。轮到千里眼的时间是晚上,我用一个开关来确定什么时候轮到谁。
现在,在开始千里眼的回合之前,我检查他是否还活着,如果是的话,我只是跳过他的回合:
case 2:
clairvoyant(); // Clairvoyant's turn
Toast.makeText(Game.this, String.valueOf(clairvoyant.getLifeStatus()), Toast.LENGTH_SHORT).show();
if(clairvoyant.getLifeStatus()) {
/* --- Let him choose ---*/
Intent intent = new Intent(this, ListPlayersClairvoyant.class);
Bundle bundle = new Bundle();
bundle.putSerializable("PLAYERS", (Serializable) players);
intent.putExtras(bundle);
startActivityForResult(intent, REQUEST_CLAIRVOYANT);
/* --------------------- */
}
nightInsideCounter++;
if(medium == null) {
nightInsideCounter++;
}
if(guard == null) {
nightInsideCounter++;
}
break;
然而,我添加了 Toast 来查看何时杀死玩家是否还活着,但即使列表中搜索的玩家被杀死,之前创建的千里眼玩家也不会被杀死。
所以基本上,列表中的玩家凭借千里眼卡,is dead; but 透视玩家,在开始轮到之前初始化以获取对它的引用,还活着!
我不明白为什么。我有什么遗漏的吗?我是否已经做了参考?在这种情况下,我应该如何创建对它的引用?
EDIT:
现在一切正常,我已经在 Player 类和 Card 类上实现了 Parcelable 接口。
问题是,当我从 ListPlayersVote Activity 到达游戏(主要活动)的意图时,应用程序崩溃并收到此错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String java.lang.Object.toString()”
在 android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
[...] // 许多其他链接,已跳过
这是一个简单的 NullPointer 异常,但我没有得到错误代码的链接,而是得到了其他脚本的所有其他链接(可能已经制作了,不是我做的),这让我陷入困境,我该如何修复如果我没有得到任何指向我的代码的链接,会出现错误吗?
资源
投票活动 http://pastebin.com/yryzgzBq
玩家等级 http://pastebin.com/B5GTSnEF
我如何检索传递的 ArrayLists 并检查它们的值(这是 StartActivityForResult() ):
/* ------------------------------------------ */
/* ---------- If they want to VOTE ---------- */
/* ------------------------------------------ */
if(requestCode == REQUEST_VOTE) {
if(resultCode == Activity.RESULT_OK) {
// Get the Player clicked
ArrayList<Player> highestList = data.getParcelableArrayListExtra("HIGHEST");
ArrayList<Player> highestList1 = data.getParcelableArrayListExtra("HIGHEST1");
System.out.println("[5] The players of the first list are " + highestList.size() + ".");
System.out.println("[6] The players of the second list are " + highestList1.size() + ".");
// Add the most voted players to a signle List, highest
highest.addAll(highestList);
highestList.clear();
highest.addAll(highestList1);
highestList1.clear();
// Write the names in chat
write("Il villaggio ha i propri sospettati:");
for (Player player : highest){
write(player.getName());
}
System.out.println("[7] The players chosen are " + highest.size() + ".");
highest.clear();
} else if (resultCode == Activity.RESULT_CANCELED) {
// Some stuff that will happen if there's no result
}
}