我有这个纸牌游戏,我将所有玩家存储在一个List.为了找出我想与谁一起行动的玩家,每个玩家都有一个Card(我可以得到卡片名称),a name(我可以得到玩家的名字),但每个玩家都有一个独特的ID.
现在,在我的onCreate()方法开始时,我找到并将列表的播放器分配给播放器Player clairvoyant:
public void initializeCharacters() {
for (Player player : players) {
if (player.getCardName().equals("Clairvoyant")) {
clairvoyant = player;
}
}
Run Code Online (Sandbox Code Playgroud)
游戏在夜晚和白天之间切换.透视力的转折是在夜间,我用一个开关来确定何时轮到谁.
现在,在开始千里眼之前,我会检查他是否还活着,在否定的情况下,我只是跳过他的回合:
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;
Run Code Online (Sandbox Code Playgroud)
然而,我添加了Toasts,看看当我还活着的时候我会杀死玩家,但即使在列表中搜索到的玩家被杀死,先前创建的Clairvoyant玩家也不会.
所以基本上,带有透视牌的名单中的玩家已经死了 ; 但是,在开始轮到他之前初次确定的透视球员仍然活着!
我不明白为什么.有什么我想念的吗?那是我做过参考吗?在这种情况下,我应该如何创建它的引用?
编辑:
现在一切正常,我已经在Player Class和Card上实现了Parcelable接口.问题是当我从ListPlayersVote Activity到第一个游戏(主要的一个)的意图时,应用程序崩溃并且我收到此错误:
java.lang.NullPointerException:尝试在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)上的空对象引用上调用虚方法'java.lang.String java.lang.Object.toString()'
[...] //许多其他链接,跳过
这是一个简单的NullPointer异常,但我没有链接到我的错误代码,而是我得到了其他脚本的所有其他链接(可能已经制作,而不是我)这让我卡住了,我怎么想修复如果我没有得到任何链接到我的代码?
我如何检索传递的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
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我从 ListPlayersVote Activity 到达游戏(主要活动)的意图时,应用程序崩溃并收到此错误:
正如我在评论中所说,ArrayAdapter当适配器与其中包含空值的数据列表一起使用时,会发生与该类相关的异常。查看您的代码,罪魁祸首似乎是一个 Player 对象,该对象没有在ListPlayersVote类中初始化其字段。在该类中,您当前的代码几乎总是会引入未初始化的 Player 对象引用。在ListPlayersVote的onCreate()回调中,您首先将两个 Player 对象添加到持有者列表中,我假设将其作为进一步计算的基础:
// remove the lines below , there's no need for them
// also simply doing new Player() will result in a Player which doesn't have
// its fields initialized(and you don't update them later either)
highestList.add(new Player()); // is there a reason for adding this empty players?
highestList1.add(new Player());
Run Code Online (Sandbox Code Playgroud)
然后在 onEntryClick() 回调中更新您的代码以正确处理玩家投票的计数:
for (Player player1 : players){ // For each player
// first of all you need to move the initialization for highest and highest1 INSIDE the
// for loop to use the proper highest values at each iteration
// With your current code the lists highestlist and highestlis1 each have
// ONE Player object which is empty (because in the default constructor
// of Player you do nothing), you then use the empty initial Player in each list to
// compare the vote count, this will mostly result in the first if
// clause(playerCount > highest) being triggered,
// which will add the initial empty Player object to highestlist1
int highest = highestList.size() == 0 ? -1 : highestList.get(0).getCount();// if the list is empty initialize it with -1 to signal the first player handled
int highest1 = highestList1.size() == 0 ? -1 : highestList1.get(0).getCount();
int playerCount = player1.getCount(); // Get his votes
// you need to check for -1 to handle the case when you're dealing
// with the first player( which of course will have the highest
// count as there's no either player to compare it yet)
if(playerCount > highest || highest == -1){ // If they're the highest so far, add it to the 1st list
highestList1.addAll(highestList);
highestList.clear();
highestList.add(player1);
// the same for highest1
} else if (playerCount > highest1 || highest1 == -1){ // If they're the 2nd highest so far, add it to the 2nd list
highestList1.clear();
highestList1.add(player1);
}else if(playerCount == highest){ // If they're equal to the current highest one, add it to the 1st list as well
highestList1.add(player1);
} else if (playerCount == highest1){ // If they're equal to the current 2nd highest one, add it to the 2nd list as well
highestList1.add(player1);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
573 次 |
| 最近记录: |