1

I mean, I understand the error, but I don't see the error on Android Lollipop nor Marshmallow, only on KitKat.

I am sending a bidimensional array from an activity to another through an intent, like this:

String[][] bidimensionalArray = new String[3][3];

public void switchActivity(MenuItem item) {
    Intent intent = new Intent(this, myActivity.class);
    intent.putExtra("bidimensionalArray", bidimensionalArray);
    startActivity(intent);
}

And, on the receiving activity, I am doing this:

bidimensionalArray = (String[][]) getIntent().getSerializableExtra("bidimensionalArray");

Again, this works perfectly on Android 5 and 6, but is erroring out on Android 4.

What changed with regards to serialization from Lollipop onward?

user1301428
  • 1,743
  • 3
  • 25
  • 57
  • The question you should be asking, is "What changed with serialization from Lollipop onward?" – Darth Android Mar 16 '16 at 15:18
  • @DarthAndroid fair enough, I'll update the question :) – user1301428 Mar 16 '16 at 15:19
  • 2
    It appears to be a bug that has now been fixed. http://stackoverflow.com/questions/28720062/class-cast-exception-when-passing-array-of-serializables-from-one-activity-to-an You can get around it by getting it as an `Object[][]`, checking the type using `instanceof`, and copying it into a new `String[][]` if necessary. – Paul Boddington Mar 16 '16 at 15:33
  • 1
    @PaulBoddington thanks, copying the array seems to be the only solution. I had to get it as `Object[]` though, otherwise I would get the same error :/ – user1301428 Mar 16 '16 at 15:56
  • @user1301428 It's very strange. Android deserialization is a disaster - it's full of bugs and even when it works it does strange things - e.g. turns a TreeMap into a HashMap. – Paul Boddington Mar 16 '16 at 16:00
  • @PaulBoddington oh right, so it wasn't just me lol. I started thinking I was doing things wrong when getting weird results, but apparently I am not alone then, good to know :D – user1301428 Mar 16 '16 at 16:01

1 Answers1

1

So apparently this is a bug in Android 4 that has not been fixed yet. For the record, I was trying this on Android 4.3.1.

I solved this by getting the array as an Object[] array and copying it to a String[][] array like this in the receiving activity:

private Object[] arrayDataObject;
private String[][] arrayData;

arrayDataObject = (Object[]) getIntent().getSerializableExtra("myArray");
arrayData = Arrays.copyOf(arrayDataObject,arrayDataObject.length,String[][].class);
user1301428
  • 1,743
  • 3
  • 25
  • 57