0

I am creating an application in which I have a "Game" object which holds some information about the game. Here is my code

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];
        }
    };
}

Then in my 1st Activity (A) I create a Game:

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);

And then I try to get it

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());
}

For some reason the output from the System.out in activity A is correct and shows that my game creation works:

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}}

but in my mainActivity I get:

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)

As you can see it finds objects with very strange IDs and at some point runs out of memory. Any ideas what is going on ?

Thank you

mp3por
  • 1,796
  • 3
  • 23
  • 35
  • problem is with your game object that is you sending with intent. you can't send object of larger size with intent. you have to trim your object. only put necessary information with intent then get it. – Zeeshan Shabbir Dec 21 '15 at 17:32
  • How do I send it then? The object is at bare minimum right now ... It will get bigger with time ... what do you propose ? – mp3por Dec 21 '15 at 19:57
  • Also I am getting "ClassNotFound" exception ... – mp3por Dec 21 '15 at 20:43

0 Answers0