【问题标题】:How do you send an Array of custom objects between activities?如何在活动之间发送自定义对象数组?
【发布时间】:2013-09-04 20:24:09
【问题描述】:

我已经找到了很多他们发送 ArrayList 的地方,但我需要能够将我的自定义对象“Card”的简单数组从活动 A 发送到 B。我相信我已经以允许的方式制作类我的对象作为 Intent 的额外内容传递。这是我的 Parcelable 类卡片:

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];
    }
};  
}

这就是我将它添加到活动 A(即主要活动)中的意图的方式:

    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);

然后我在 Activity B 的 onCreate 方法中重新调用它:

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

unfourntunatley 应用程序在到达 Activity B 中的代码时立即崩溃。我是否以错误的方式称呼它? 这是日志猫

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

【问题讨论】:

  • 请张贴logcat。它包含一个堆栈跟踪,它将准确地告诉您是哪一行导致了崩溃。
  • 一切看起来都不错,我唯一能想到的(并且不确定它是否会有所帮助)就是隐含地将数组放入意图中。我的意思是制作一个 Bundle,在其上调用 putParcelableArray,然后使用 putExtras() 将包放入 Intent
  • 对不起,logcat的格式,Code-Guru我无法将其作为图像上传
  • 您可以使用代码块而不是块引用来使 logcat 看起来更好。
  • 如果 parcelable[] 是在ActivityResult 上传递的,请注意数据可作为输入参数,而 getIntent().. 由于某种原因会传递一个空指针。

标签: java android arrays object android-intent


【解决方案1】:

你不能像那样转换数组。如果你有一个Parcelable 的数组并且你想将它转换为一个Card 的数组,你需要循环遍历这个数组并分别转换每个对象:

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];
}

【讨论】:

  • 如果你想避免这种情况,尽可能使用arraylist,你可以强制转换...
【解决方案2】:

我的解决方案是:

allCards = in.createTypedArray(Card.CREATOR);

【讨论】:

    猜你喜欢
    • 2011-10-15
    • 2011-08-03
    • 1970-01-01
    • 2016-09-10
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多