我需要将一个对象从一个活动发送到另一个活动;我读到一种方法是将parcelable接口实现到我的对象中。 我安排了一些代码,但我想我搞砸了。 这是我的代码的一部分。 我要发送的对象:
public class Profilo implements Parcelable {
String nome;
String cognome;
....
public Profilo(String nome,String cognome,String dataNascita,String luogoNascita,String problematica,String dettagli)
{
this.nome = nome;
this.cognome = cognome;
....
}
public Profilo(Parcel parcel) {
this.nome = parcel.readString();
this.cognome = parcel.readString();
}
....
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(nome);
dest.writeString(cognome);
}
public final static Parcelable.Creator CREATOR = new Parcelable.Creator() {
@Override
public Profilo createFromParcel(Parcel source) {
return new Profilo(source);
}
@Override
public Profilo[] newArray(int size) {
return new Profilo[size];
}
};
}
然后发送对象的活动(Profilo对象的ArrayList):
public class ProfileFragment extends Fragment {
public ArrayList<Profilo>profili = new ArrayList<Profilo>(10);
....
final Button Login = (Button) getView().findViewById(R.id.button2);
Login.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(cont, LoginChooser.class);
Bundle bundle = new Bundle();
bundle.putParcelable("profilo", (Parcelable) profili);
intent.putExtras(bundle);
startActivity(intent);
}
});
然后接收对象的活动:
public class LoginChooser extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_chooser);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList ListaProfili = (ArrayList)bundle.getParcelable("punto");
Profilo listaProfili[] = (Profilo[]) ListaProfili.toArray();
String nomeProfilo[] = new String[10];
int index = 0;
for(Profilo elem : listaProfili) {
if(elem!= null) {
nomeProfilo[index] = elem.getNome() + "" + elem.getCognome();
index++;
}
}
}
}
也许我犯了完全错误,无论如何我认为我的错误可能就在这里:
bundle.putParcelable("profilo", (Parcelable) profili);
我很乐意接受任何建议和解释
这是堆栈跟踪
03-10 18:54:37.484: E/AndroidRuntime(2262): FATAL EXCEPTION: main
03-10 18:54:37.484: E/AndroidRuntime(2262): Process: info.androidhive.slidingmenu, PID: 2262
03-10 18:54:37.484: E/AndroidRuntime(2262): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.LoginChooser}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to info.androidhive.slidingmenu.Profilo[]
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.os.Handler.dispatchMessage(Handler.java:102)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.os.Looper.loop(Looper.java:137)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-10 18:54:37.484: E/AndroidRuntime(2262): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 18:54:37.484: E/AndroidRuntime(2262): at java.lang.reflect.Method.invoke(Method.java:515)
03-10 18:54:37.484: E/AndroidRuntime(2262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-10 18:54:37.484: E/AndroidRuntime(2262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-10 18:54:37.484: E/AndroidRuntime(2262): at dalvik.system.NativeStart.main(Native Method)
03-10 18:54:37.484: E/AndroidRuntime(2262): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to info.androidhive.slidingmenu.Profilo[]
03-10 18:54:37.484: E/AndroidRuntime(2262): at info.androidhive.slidingmenu.LoginChooser.onCreate(LoginChooser.java:21)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.app.Activity.performCreate(Activity.java:5243)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-10 18:54:37.484: E/AndroidRuntime(2262): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-10 18:54:37.484: E/AndroidRuntime(2262): ... 11 more
答案 0 :(得分:1)
使用
bundle.putParcelableArrayList("profilo", profili);
并以这种方式阅读:
ArrayList<Profilo> profili = getIntent().getExtras().getParcelableArrayList("profilo");