2

I'm trying to send a 2D array through an Intent by using Bundle.putSerializable, but am getting a ClassCastException error when retrieving it.

The code I'm using is more or less exactly what other StackOverflow users have reported as working for them, so I'm not certain why I'm getting this error.

I have a 2D Array which I add to my Bundle and Intent as follows:

String[][] myString= new String[myGroup.length][myGroup[0].length];
//Data is added to String here
Bundle myBundle = new Bundle();
myBundle.putSerializable("myString", myString);
intent.putExtras(myBundle);

And retrieve in the next Activity as follows:

Bundle myBundle = getIntent().getExtras();
String[][] myNewString= (String[][]) myBundle.getSerializable("myString");

However, at Runtime I get the error:

java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[[][]

And I'm not certain why, as other users seemingly reported success with this. Would I be better served taking another approach?

Thanks in advance.

2
  • What API level is the error generally appear on? Commented Jul 24, 2014 at 13:20
  • It's being run on rooted 4.4, so level 19 I believe. Commented Jul 24, 2014 at 13:33

1 Answer 1

2

Hope this help....

Retrieve data in the next Activity as

String[][] myNewString=null;
Object[] objectArray = (Object[]) getIntent().getExtras().getSerializable("myString");
if(objectArray!=null){
    myNewString = new String[objectArray.length][];
    for(int i=0;i<objectArray.length;i++){
        myNewString[i]=(String[]) objectArray[i];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This was the solution I was getting ready to implement; however, I'm curious why casting straight to a String[][] was reported to work for some people whereas it clearly wasn't for me. Any idea?
This works and I'm marking it as the correct answer. Still curious why it's necessary though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.