java.lang.ClassCastException error when using getSerializable () in API 16 [closed]

1

I have 2 emulators, Nexus 5 API 23 and Nexus One API 16. In Nexus 5 the program runs right, in the Nexus One the crash program and the error it gives is Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to int[][]

It gives error here: meuArray=(int[][]) extras.getSerializable("array");

How can the same code do well in one emulator and bad in the other? Does it have to do with the API's?

EDIT:

int[][] position = new int[5][3];

if(sentado[indice].isChecked()){//Esta parte tem mais if´s mas fazem a mesma coisa que este, so varia os indices
    position[0][0]++;}

Bundle bundle = new Bundle();
bundle.putSerializable("array", position);
intent.putExtras(bundle);
    
asked by anonymous 16.12.2016 / 01:05

1 answer

1

According to this answer in SOen seems to be a bug in Android 4.

The suggested way to resolve is to cast the method return to Object[] and use the Arrays.copyOf() method to get the original array :

>
Object[] vector;
int[][] meuArray;

vector = (Object[])extras.getSerializable("array");
meuArray = Arrays.copyOf(vector, vector.length, int[][].class);
    
16.12.2016 / 16:14