Hey all,
I've been working on my first app (card game) and almost everything
works fine when testing on my Samsung Epic 4G except for this one
thing, the following scenario which I can consistently reproduce:
Launch Game
Click the home button and launch ~10 other applications until the game
is forced out of memory
Relaunch Game
>From logcat I can see that the game gets a NullPointerException in
OnCreate when it tries to read the bundle. But normally this works
fine (when saving state and resuming), bundle is saved in Activity's
saveInstanceState as follows:
protected void onSaveInstanceState(Bundle b) {
b.putParcelable(this.gameState.getClass().getName(),
this.gameState);
super.onSaveInstanceState(b);
}
and retrieved in Activity's OnCreate as follows:
if (bundle != null && !bundle.isEmpty()) {
gameState = (GameState)
bundle.getParcelable(GameState.class
.getName());
gameState.setState(State.REGULAR);
}
Here are the relevant parts from GameState:
public class GameState implements Parcelable {
public void writeToParcel(Parcel out, int flags) {
out.writeInt(shufflesLeft);
for (int row = 0; row < Card.NUM_SUITS; row++) {
out.writeTypedArray(this.board[row], 0);
}
}
public static final Parcelable.Creator<GameState> CREATOR = new
Parcelable.Creator<GameState>() {
public GameState createFromParcel(Parcel in) {
return new GameState(in);
}
public GameState[] newArray(int size) {
return new GameState[size];
}
};
private GameState(Parcel in) {
shufflesLeft = in.readInt();
for (int row = 0; row < Card.NUM_SUITS; row++) {
in.readTypedArray(this.board[row], Card.CREATOR);
}
}
And Card:
public class Card implements Parcelable {
public void writeToParcel(Parcel out, int flags) {
out.writeInt(this.suit.getValue());
out.writeInt(this.rank.getValue());
}
public static final Parcelable.Creator<Card> CREATOR
= new Parcelable.Creator<Card>() {
public Card createFromParcel(Parcel in) {
return new Card(in);
}
public Card[] newArray(int size) {
return new Card[size];
}
};
private Card(Parcel in) {
this.suit = Suit.fromInt(in.readInt());
this.rank = Rank.fromInt(in.readInt());
}
Thanks so much for your help!
James Wright
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en