我有这个纸牌游戏,我将所有玩家存储在一个 List
中。
为了找出我想和谁一起行动的玩家,每个玩家都有一张Card
(我可以得到卡片名称),一个name
(我可以得到玩家的名字) , 但为了唯一起见,每个玩家都有一个 ID
。
现在,在我的 onCreate()
方法的开头,我找到并将列表中的一个播放器分配给一个播放器 Player
千里眼
:
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;
然而,我添加了 Toasts 来查看我何时杀死玩家是否还活着,但即使在列表中搜索到的玩家已被杀死,之前创建的 Clairvoyant 玩家也不会。
所以基本上,列表中的玩家拥有千里眼卡,已经死了;但是千里眼玩家,在开始他的回合之前初始化以引用它,还活着!
我不明白为什么。我有什么想念的吗?是我做了引用还是没做?在这种情况下,我应该如何创建对它的引用?
编辑:
现在一切正常,我已经在 Player 类和 Card 类上实现了 Parcelable 接口(interface)。 问题是当我从 ListPlayersVote Activity 到游戏 Activity (主要 Activity )的 Intent 时,应用程序崩溃并且我收到此错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
[...] // Many other links, skipped
这是一个简单的 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
}
}
最佳答案
看来您正在寻找 weak reference :如果不存在对该对象的其他引用,则弱引用也会丢失。参见 Reference也是。
WeakReference<Player> clairvoyant;
// Initialisation of clairvoyant from players:
// So do this whenever players is filled.
for (Player player : players) {
if (player.getCardName().equals("Clairvoyant")) {
clairvoyant = new WeakReference<>(player);
}
}
// Everywhere where clairvoyant is inspected:
Player player = clairvoyant.get();
if (player != null) {
// There is a clairvoyant player:
,,,
}
虽然没有 Android 的 java 行为经验。
关于java - 在 Java 中传递引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37924789/