1

I have my own Object SineWave which I need to pass from SettingsActivity to SimulationActivity

But it gives a classcastexception when I cast SineWave[] to Parceable[].

SineWave object:-

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 :-

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.

Dyna
  • 2,304
  • 1
  • 14
  • 25
udiboy1209
  • 1,472
  • 1
  • 15
  • 33

1 Answers1

1

You cannot cast an object array (which is what waveList.toArray() gives you) directly to an array of another type. You only need to cast to SineWave too because it already is a 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:

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".

I don't know what impact that might have, but you may need to add your package name in front of "list" as well.

NigelK
  • 8,255
  • 2
  • 30
  • 28
  • but waveList is already an ArrayList of SineWave s so toArray() should give SineWave[] right? why do i need to cast it to the same class again? – udiboy1209 Nov 18 '13 at 14:57
  • No, toArray() always returns a new array of type Object[]. Hover your mouse over it and intellisense should show you that. – NigelK Nov 18 '13 at 15:02