7

I Have found many places where they send an ArrayList but I need to be able to send a simple Array of my custom object "Card" from activity A to B. I beleive I have made the classes in a way that will allow my Object to be passed as an extra from an Intent. This is my Parcelable class Card:

import android.os.Parcel;
import android.os.Parcelable;

public class Card implements Parcelable{
   public String suit;
   public int value;
   public String color;

public Card(String suit, int value){
    this.suit = suit;
    this.value = value;

    if(suit == "hearts" || suit == "diamonds"){
        this.color = "red";
    }else{
        this.color = "black";
    }
}

public String toString(){
    String s = suit;
    return s;
}

 private Card(Parcel in) {
     value = in.readInt();
     color = in.readString();
     suit = in.readString();
    }

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel out, int flags) {
    out.writeInt(value);
    out.writeString(color);
    out.writeString(suit);
}

public static final Parcelable.Creator<Card> CREATOR
        = new Parcelable.Creator<Card>() {
    public Card createFromParcel(Parcel in) {
        return new Card(in);
    }

    public Card[] newArray(int size) {
        return new Card[size];
    }
};  
}

and this is how I am adding it to the intent in the activity A (e.i. the main activity):

    Card[] myCardArray = new Card[7];
    //Note the card array is filled before starting the intent.
    Intent intnt = new Intent(this, B.class);
    intnt.putExtra("array", myCardArray);
    startActivity(intnt);

I am then retriving it in Activity B's onCreate method as so:

    allCards = (Card[]) getIntent().getExtras().getParcelableArray("array");

unfourntunatley the app is crashing as soon as it reaches the code in Activity B. Am I calling it the wrong way? Here is the logcat

09-05 15:27:37.007: E/Trace(26971): error opening trace file: No such file or directory (2)
09-05 15:27:37.007: D/ActivityThread(26971): setTargetHeapUtilization:0.25
09-05 15:27:37.007: D/ActivityThread(26971): setTargetHeapIdealFree:8388608
09-05 15:27:37.007: D/ActivityThread(26971): setTargetHeapConcurrentStart:2097152
09-05 15:27:37.228: D/libEGL(26971): loaded /system/lib/egl/libEGL_adreno200.so
09-05 15:27:37.228: D/libEGL(26971): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
09-05 15:27:37.228: D/libEGL(26971): loaded /system/lib/egl/libGLESv2_adreno200.so
09-05 15:27:37.248: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:37.268: E/(26971): <s3dReadConfigFile:75>: Can't open file for reading
09-05 15:27:37.268: E/(26971): <s3dReadConfigFile:75>: Can't open file for reading
09-05 15:27:37.268: D/OpenGLRenderer(26971): Enabling debug mode 0
09-05 15:27:39.640: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:41.963: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:42.844: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:44.425: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:46.257: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:47.799: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:49.781: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:51.182: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:51.943: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:55.237: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:56.468: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:57.910: I/Adreno200-EGLSUB(26971): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 15:27:59.101: D/AndroidRuntime(26971): Shutting down VM
09-05 15:27:59.101: W/dalvikvm(26971): threadid=1: thread exiting with uncaught exception (group=0x412e8438)
09-05 15:27:59.111: E/AndroidRuntime(26971): FATAL EXCEPTION: main
09-05 15:27:59.111: E/AndroidRuntime(26971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marco.pokerhands2/com.marco.pokerhands2.DisplayHands}: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.marco.pokerhands2.Card[]
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.app.ActivityThread.access$700(ActivityThread.java:139)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.os.Looper.loop(Looper.java:137)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.app.ActivityThread.main(ActivityThread.java:4918)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at java.lang.reflect.Method.invokeNative(Native Method)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at java.lang.reflect.Method.invoke(Method.java:511)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at dalvik.system.NativeStart.main(Native Method)
09-05 15:27:59.111: E/AndroidRuntime(26971): Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.marco.pokerhands2.Card[]
09-05 15:27:59.111: E/AndroidRuntime(26971):    at com.marco.pokerhands2.DisplayHands.onCreate(DisplayHands.java:30)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.app.Activity.performCreate(Activity.java:5048)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-05 15:27:59.111: E/AndroidRuntime(26971):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052)
09-05 15:27:59.111: E/AndroidRuntime(26971):    ... 11 more
5
  • Please post the logcat. It contains a stack trace which will tell you exactly which line causes the crash. Commented Sep 4, 2013 at 20:42
  • Everything looks good, the only thing I can think of (and not sure it will help) is to be implicit about putting the array into the intent. By which I mean making a Bundle, calling putParcelableArray on it, and then putting the bundle into the Intent using putExtras() Commented Sep 4, 2013 at 20:56
  • Sorry about the format of the logcat, Code-Guru I was not able to upload it as an image Commented Sep 5, 2013 at 19:31
  • You can use a code block instead of blockquote to make the logcat look much better. Commented Sep 5, 2013 at 19:34
  • If the parcelable[] is delivered onActivityResult, note that the data is available as input parameter, while getIntent().. for some reason will deliever a nullpointer. Commented Mar 17, 2014 at 9:33

2 Answers 2

22

You can't cast arrays like that. If you have an array of Parcelable and you want to cast it to an array of Card, you need to loop through the array and cast each object separately:

Parcelable[] allParcelables = getIntent().getExtras().getParcelableArray("array");
Card[] allCards = new Card[allParcelables.length];
for (int i = 0 ; i < allParcelables.length; i++) {
    allCards[i] = (Card)allParcelables[i];
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you want to avoid this, use arraylist if possible, you can cast that...
0

My Solution would be:

allCards = in.createTypedArray(Card.CREATOR);

Comments

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.