I've this cards game, in which I store all the players in a List
.
To find out who's the player I want to act with, each player has a Card
(I can get the card name), a name
(I can get the player's name), but to be unique each player has an ID
.
Now, at the beginning of my onCreate()
method, I find, and assign a player of the list to a Player, Player
clairvoyant
:
public void initializeCharacters() {
for (Player player : players) {
if (player.getCardName().equals("Clairvoyant")) {
clairvoyant = player;
}
}
The game switch between Night and Day. The Clairvoyant's turn is during the night, and I used a switch to determine when's who's turn.
Now, before starting the Clairvoyant's turn, I check if he's alive or not, in negative case, I simply skip his turn:
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;
And yet, I've added Toasts to see when I kill players if they're still alive, but even if the Player searched in the list is killed, the Clairvoyant player, previously created isn't.
So basically, the player in the list with the clairvoyant card, is dead; but the clairvoyant player, initialized before to have a reference to it before starting his turn, is still alive!
I don't understand why. Is anything I'm missing? Is that I've done a reference or not? In this case, how should I create a reference to it?
EDIT:
Now it all works fine, I have implemented the Parcelable interface on the Player Class and on the Card one too. The problem is when I get to the intent from ListPlayersVote Activity to the Game one (the main one), the App crashes and I get this error:
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
Which is a simple NullPointer exception, but I get no links to my code for the error, instead I get all other links to other scripts (Probably already made, not by me) and this keeps me stuck, how am I suppose to fix the bug if I don't get any link to my code?
Resources
How I retrieve the ArrayLists passed, and check for their values (this is a 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
}
}