简体   繁体   English

尝试将实现可拆分的自定义对象传递给使用Intent的另一个活动时发生ClassCastException

[英]ClassCastException when trying to pass custom object implementing parcelable to another activity using Intent

I have my own Object SineWave which I need to pass from SettingsActivity to SimulationActivity 我有自己的对象SineWave ,需要将其从SettingsActivity传递给SimulationActivity

But it gives a classcastexception when I cast SineWave[] to Parceable[]. 但是当我将SineWave []转换为Parceable []时,它给出了classcastexception。

SineWave object:- SineWave对象:-

public class SineWave implements Parcelable{
float A,k,w,phi,VELOCITY;
int waveNum;
boolean plot;

public SineWave(Parcel in){
    A=in.readFloat();
    k=in.readFloat();
    w=in.readFloat();
    phi=in.readFloat();
    VELOCITY=in.readFloat();
    waveNum=in.readInt();
    plot=in.readInt()==1;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeFloat(A);
    out.writeFloat(k);
    out.writeFloat(w);
    out.writeFloat(phi);
    out.writeFloat(VELOCITY);
    out.writeInt(waveNum);
    out.writeInt(plot ? 1 : 0);
}

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

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

SettingsActivity :- 设置活动:-

waveList = new ArrayList<SineWave>();

@Override
public void onClick(View view){
    switch(view.getId()){
        case R.id.start:
            Intent i1 = new Intent();
            i1.setClass(this, SimulationActivity.class);
            i1.putExtra("list",(Parcelable[])(waveList.toArray())); 
            //Gives ClassCastException at above line
            startActivity(i1);
            break;
        case R.id.add_wave: 
            ...
            break;
        default: break;
    }
}

SimulationActivity :- SimulationActivity:-

public void onCreate(Bundle icicle){
    ...
    Intent i = getIntent();
    SineWave[] waveList[]=(SineWave[])i.getParcelableArrayExtra("list");
    ...
}

The error stack trace:- 错误堆栈跟踪:

Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
        at com.meet.simulators.wavesonstringsimulator.SettingsActivity.onClick(SettingsActivity.java:37)            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at android.view.View$1.onClick(View.java:2139)
            at android.view.View.performClick(View.java:2485)
            at android.view.View$PerformClick.run(View.java:9080)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

Can't understand what the problem is? 不明白是什么问题? Please Help. 请帮忙。

You cannot cast an object array (which is what waveList.toArray() gives you) directly to an array of another type. 您不能将对象数组(即waveList.toArray()为您提供的对象)直接转换为其他类型的数组。 You only need to cast to SineWave too because it already is a Parcelable. 您也只需要强制转换为SineWave,因为它已经是一个Parcelable。 I believe this is how you can do it: 我相信这是您可以做到的:

i1.putExtra("list", waveList.toArray(new SineWave[waveList.size()]));

For info, the documentation for putExtra states: 有关信息,putExtra的文档指出:

Add extended data to the intent. 将扩展数据添加到该意图。 The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll". 该名称必须包含包前缀,例如,应用程序com.android.contacts将使用“ com.android.contacts.ShowAll”之类的名称。

I don't know what impact that might have, but you may need to add your package name in front of "list" as well. 我不知道可能会有什么影响,但是您可能还需要在“列表”前面添加包名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将实现Parcelable的对象从一个活动传递到另一个活动 - Pass Object implementing Parcelable from one Activity to Another Activity 为数字对象实现可拆分时的ClassCastException - ClassCastException When Implementing Parcelable for Number Object 如何使用 android 中的 Parcelable 接口通过意图将包含可绘制参数的 object 传递给另一个活动 - how to pass an object containing with drawable parameter to another activity through intent using Parcelable Interface In android android-无法将Intent中的包裹传递给另一个活动 - android - unable to pass parcelable in Intent to another activity 在类中实现可包裹性以将其从活动传递到另一个活动 - Implementing parcelable in a class to pass it from an activity to another 使用意图将可包裹对象内的可包裹对象列表发送到另一个活动 - Send List of Parcelable Object Inside Parcelable Object to Another Activity Using Intent 使用Parcelable将包含对象列表的对象传递给另一个活动 - Pass an object containing list of objects to another activity using Parcelable 如何使用宗地将CountDownTimer对象传递给Intent? - How to pass object of CountDownTimer into intent using parcelable? 将对象传递到另一个活动(没有Parcelable?) - Pass object to another activity (without Parcelable?) 将自定义对象列表传递给活动:parcelable或serializable? - Pass a custom object list to an activity: parcelable or serializable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM