java.lang.Classcastexception error when using getSerializable() in API 16

Asked

Viewed 69 times

1

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

Makes a mistake here: meuArray=(int[][]) extras.getSerializable("array");

How is it possible for the same code to be good in one emulator and bad in the other? It has to do with 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);
  • The problem must be before that, it must be in put, only if you have an example from the put.

  • I did another test, ran the program on 2 Nexus One emulators, only one had API 16 and one had API 23. In what had API 23 came out right, what had API 16 crashed and got some parts of the layout deformed. So I think it should be even from the @Guilhermenascimento API

  • As I said the problem is at the time of put Probably, without code you can’t be sure how it triggers, I mean it’s the emulator’s fault, but it’s put and not get, if you don’t put the code, you can’t be sure. http://answall.com/help/mcve

  • already posted some code @Guilhermenascimento

  • I believe the problem is that java.lang.Object[] should be java.lang.Object[][], maybe some values are null

  • I boot all the variables(I didn’t post because it was too big) so I guess none is null @Guilhermenascimento

  • That int position = new int[5][3]; shouldn’t be that int[][] position = new int[5][3];?

  • Yeah, I just forgot to move over here, in my code it’s right @Guilhermenascimento

Show 3 more comments

1 answer

1


According to this reply on Soen seems to be a bug on Android 4.

The suggested way to solve is to do the cast return of the method to Object[] and use the method Arrays.copyOf() to obtain the array original:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.