ACTIVITY 1:
Bundle bundle = new Bundle();
bundle.putSerializable("CustomLevelData", LevelCreator.LCLevelData);
Intent i = new Intent(LevelCreatorPopout.this, GameView.class);
i.putExtras(bundle);
startActivity(i);
ACTIVITY 2:
LevelData=(int[][]) extras.getSerializable("CustomLevelData");
ERROR:
E/AndroidRuntime(16220): FATAL EXCEPTION: main
E/AndroidRuntime(16220): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.powerpoint45.maze/com.powerpoint45.maze.GameView}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to int[][]
I have searched but found nothing on 2d INT array passing
If you still want to stick with Serializable, you have to break it in the second activity and do a loop.
Bundle dataBundle = getIntent().getExtras();
int[][] levelData = new int[3][3];
Object[] temp = (Object[])dataBundle.getSerializable("CustomLevelData");
if(temp != null){
levelData = new int[temp.length][];
for(int i = 0; i < temp.length; i++){
levelData[i] = (int[])temp[i];
}
}
Instead of Serializable, it's better to use Parcelable from a performance point of view to pass non- primitive data.
Not sure if this is the best idea, but you can define a class which contains the 2d-array and which implements Parcelable. You can then pass an instance of that class from an activity using:
Intent intent = this.getIntent();
// Assume MyClass is the class which contains the 2d-array
intent.putExtra("key", myclassObj); //value being the instance/object of MyClass that you want to pass
You can retrieve it in another activity using:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
MyClass mc = (MyClass)bundle.getParcelable("key");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With