我正在创建一个应用程序,其中我有一个" Game"包含有关游戏的一些信息的对象。这是我的代码
Game.java
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import java.util.Arrays;
/**
* Created by velin on 12/12/2015.
*/
public class Game implements Parcelable {
public GameConfiguration config;
private GameLevel[] levels;
private int currentLevel;
// Constructors
private Game(Parcel in) {
currentLevel = in.readInt();
config = in.readParcelable(GameConfiguration.class.getClassLoader());
// config = GameConfiguration.CREATOR.createFromParcel(in);
levels = in.createTypedArray(GameLevel.CREATOR);
// Log.d(TAG, "Game created from Parcel" + this.toString());
}
public Game(GameConfiguration config) {
this.config = config;
this.currentLevel = 0;
this.levels = new GameLevel[config.levelsConfig.length];
for (GameLevelConfiguration levelConfiguration : config.levelsConfig) {
GameLevel level = new GameLevel(levelConfiguration);
levels[levelConfiguration.levelId] = level;
}
// Log.d(TAG, "Game created" + this.toString());
}
/* Code for Parcelable*/
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(currentLevel);
dest.writeParcelable(config, 0);
dest.writeTypedArray(levels,0);
}
public static final Parcelable.Creator<Game> CREATOR = new Parcelable.Creator<Game>() {
@Override
public Game createFromParcel(Parcel source) {
return new Game(source);
}
@Override
public Game[] newArray(int size) {
return new Game[size];
}
};
@Override
public String toString() {
return "Game{" +
"config=" + config +
", currentLevel=" + currentLevel +
", levelsConfig" + levels +
'}';
}
public GameLevel[] getLevels() {
return levels;
}
}
GameLevel.java
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import .main.MainActivity;
/**
* Created by vbk20 on 25/10/2015.
*/
public class GameLevel implements Parcelable {
private final GameLevelConfiguration config;
private final int levelId;
public GameLevel(GameLevelConfiguration config) {
this.config = config;
this.levelId = config.levelId;
// Log.d(MainActivity.TAG, "GameLevel created" + this.toString());
}
private GameLevel(Parcel source) {
levelId = source.readInt();
config = source.readParcelable(GameLevelConfiguration.class.getClassLoader());
// config = GameLevelConfiguration.CREATOR.createFromParcel(source);
// Log.d(MainActivity.TAG, "GameLevel created from Parcel" + this.toString());
}
public int getLevelId(){
return levelId;
}
@Override
public String toString() {
return "GameLevel{" +
"levelId=" + levelId +
", config=" + config +
'}';
}
/* --------- PARCELABLE code --------- */
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(levelId);
dest.writeParcelable(config, 0);
}
public static final Parcelable.Creator<GameLevel> CREATOR = new Parcelable.Creator<GameLevel>() {
@Override
public GameLevel createFromParcel(Parcel source) {
GameLevel level = new GameLevel(source);
System.out.println("GameLevel createFromParcel -> " + level.levelId);
return level;
}
@Override
public GameLevel[] newArray(int size) {
System.out.println("GameLevel newArray.size = " + size);
return new GameLevel[size];
}
};
}
然后在我的第一个活动(A)中,我创建了一个游戏:
GameLevelConfiguration configLVL1 = new GameLevelConfiguration(0,10,0);
GameLevelConfiguration configLVL2 = new GameLevelConfiguration(0,20,1);
GameLevelConfiguration[] levelConfigs = new GameLevelConfiguration[]{configLVL1};
GameConfiguration gameConfig = new GameConfiguration(0,0,0,0,0,2,Utils.PHOBIAS[phobia],levelConfigs);
Game game = new Game(gameConfig);
System.out.println("ChoosePhobia game.levels.length=" + game.getLevels().length);
for(GameLevel level:game.getLevels()){
System.out.println(level.getLevelId() + "->" +level.toString());
}
Intent i = new Intent(A.this, MainActivity.class);
i.putExtra(Utils.EX_GAME,game);
startActivity(i);
然后我试着去拿它
System.out.println("MainActivity Extracting Game");
game = getIntent().getExtras().getParcelable(Utils.EX_GAME);
System.out.println("MainActivity game.gameConfig ->" + game.config);
System.out.println("MainActivity game.gameLevels.size" + game.getLevels().length);
for(GameLevel level:game.getLevels()){
System.out.println(level.getLevelId() + "->" +level.toString());
}
由于某种原因,活动A中 System.out 的输出是正确的,并显示我的游戏创建有效:
I/System.out: GameConfiguration!
I/System.out: GameConfiguration{mapImage=0, characterTopImage=0, characterBottomImage=0, characterLeftImage=0, characterRightImage=0, maxLevel=2, phobia='Spider', levelsConfig=[GameLevelConfiguration{image=0, duration=10.0, levelId=0}]}
I/System.out: ChoosePhobia game.levels.length=1
I/System.out: 0->GameLevel{levelId=0, config=GameLevelConfiguration{image=0, duration=10.0, levelId=0}}
但是在我的mainActivity中我得到了:
I/System.out: MainActivity Extracting Game
I/System.out: GameConfiguration fromParcel!
I/System.out: GameConfiguration{mapImage=0, characterTopImage=0, characterBottomImage=0, characterLeftImage=0, characterRightImage=0, maxLevel=2, phobia='Spider', levelsConfig=[GameLevelConfiguration{image=6619243, duration=1.9582157442846833E-306, levelId=7733294}]}
I/System.out: GameConfiguration createFromParcel gameConfiguration.phobia = Spider
I/System.out: GameLevel newArray.size = 7077989
I/System.out: GameLevel createFromParcel -> 6684718
I/System.out: GameLevel createFromParcel -> 6357096
I/System.out: GameLevel createFromParcel -> 3014770
I/System.out: GameLevel createFromParcel -> 4653102
I/System.out: GameLevel createFromParcel -> 7733349
I/System.out: GameLevel createFromParcel -> 6684782
I/System.out: GameLevel createFromParcel -> 7602273
E/Parcel: Class not found when unmarshalling:
java.lang.ClassNotFoundException: Invalid name:
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:308)
at android.os.Parcel.readParcelableCreator(Parcel.java:2275)
at android.os.Parcel.readParcelable(Parcel.java:2239)
at .game.GameLevel.<init>(GameLevel.java:25)
at .game.GameLevel.<init>(GameLevel.java:12)
at .game.GameLevel$1.createFromParcel(GameLevel.java:58)
at .game.GameLevel$1.createFromParcel(GameLevel.java:54)
at android.os.Parcel.createTypedArray(Parcel.java:2068)
at .game.Game.<init>(Game.java:24)
at .game.Game.<init>(Game.java:12)
at .game.Game$1.createFromParcel(Game.java:56)
at .game.Game$1.createFromParcel(Game.java:52)
at android.os.Parcel.readParcelable(Parcel.java:2246)
at android.os.Parcel.readValue(Parcel.java:2146)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2479)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelable(Bundle.java:804)
at .main.MainActivity.onCreate(MainActivity.java:60)
at android.app.Activity.performCreate(Activity.java:5941)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2375)
at android.app.ActivityThread.access$900(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5238)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
正如您所看到的,它会找到具有非常奇怪ID的对象,并且在某些时候会耗尽内存。有什么想法发生了什么?
谢谢