[英]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.