gpt4 book ai didi

android - 将对象从一个 Activity 传递到另一个 Activity 时出错(使用 Parcelable)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:57 25 4
gpt4 key购买 nike

我尝试使用 Parcelable 将对象类从一个 Activity 传递到另一个 Activity

我创建了一个类并将其命名为 Student

package com.example.test08_passobjectoverintent;

import android.os.Parcel;
import android.os.Parcelable;

public class Student implements Parcelable
{

private String st_AcadimicNumber ;
private String st_Name ;
private String st_Class ;
public String getSt_AcadimicNumber() {
return st_AcadimicNumber;
}
public void setSt_AcadimicNumber(String st_AcadimicNumber) {
this.st_AcadimicNumber = st_AcadimicNumber;
}
public String getSt_Name() {
return st_Name;
}
public void setSt_Name(String st_Name) {
this.st_Name = st_Name;
}
public String getSt_Class() {
return st_Class;
}
public void setSt_Class(String st_Class) {
this.st_Class = st_Class;
}

public Student(String st_AcadimicNumber, String st_Name, String st_Class) {
super();
this.st_AcadimicNumber = st_AcadimicNumber;
this.st_Name = st_Name;
this.st_Class = st_Class;
}
public Student() {
super();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub

}

}

在主要 Activity 中,我使用此代码传递对象 ...

                Student st = new Student("aaa","bbb","ccc");
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("stu", st);
startActivity(intent);

在第二个 Activity 中(取回一个)

我使用此代码(此处有错误)

    Student model = (Student)getIntent().getParcelableExtra("stu");

LogCat 错误

12-09 15:50:06.068: E/Trace(30655): error opening trace file: No such file or directory (2)
12-09 15:50:06.188: D/libEGL(30655): loaded /system/lib/egl/libEGL_mali.so
12-09 15:50:06.188: D/libEGL(30655): loaded /system/lib/egl/libGLESv1_CM_mali.so
12-09 15:50:06.193: D/libEGL(30655): loaded /system/lib/egl/libGLESv2_mali.so
12-09 15:50:06.193: D/(30655): Device driver API match
12-09 15:50:06.193: D/(30655): Device driver API version: 10
12-09 15:50:06.193: D/(30655): User space API version: 10
12-09 15:50:06.193: D/(30655): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Wed Sep 12 17:53:53 KST 2012
12-09 15:50:06.218: D/OpenGLRenderer(30655): Enabling debug mode 0
12-09 15:50:08.033: D/GestureDetector(30655): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0
12-09 15:50:08.123: D/AndroidRuntime(30655): Shutting down VM
12-09 15:50:08.123: W/dalvikvm(30655): threadid=1: thread exiting with uncaught exception (group=0x40e372a0)
12-09 15:50:08.128: E/AndroidRuntime(30655): FATAL EXCEPTION: main
12-09 15:50:08.128: E/AndroidRuntime(30655): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test08_passobjectoverintent/com.example.test08_passobjectoverintent.Main2Activity}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.test08_passobjectoverintent.Student
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.os.Looper.loop(Looper.java:137)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-09 15:50:08.128: E/AndroidRuntime(30655): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 15:50:08.128: E/AndroidRuntime(30655): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 15:50:08.128: E/AndroidRuntime(30655): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
12-09 15:50:08.128: E/AndroidRuntime(30655): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
12-09 15:50:08.128: E/AndroidRuntime(30655): at dalvik.system.NativeStart.main(Native Method)
12-09 15:50:08.128: E/AndroidRuntime(30655): Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.test08_passobjectoverintent.Student
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.os.Parcel.readParcelable(Parcel.java:2086)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.os.Parcel.readValue(Parcel.java:1965)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.os.Parcel.readMapInternal(Parcel.java:2226)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.os.Bundle.unparcel(Bundle.java:223)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.os.Bundle.getParcelable(Bundle.java:1165)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.content.Intent.getParcelableExtra(Intent.java:4451)
12-09 15:50:08.128: E/AndroidRuntime(30655): at com.example.test08_passobjectoverintent.Main2Activity.onCreate(Main2Activity.java:22)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.app.Activity.performCreate(Activity.java:5191)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-09 15:50:08.128: E/AndroidRuntime(30655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-09 15:50:08.128: E/AndroidRuntime(30655): ... 11 more

最佳答案

您需要实现代码以从 parcel 写入和读取您的类字段。

writeToParcel中:

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(st_AcadimicNumber);
dest.writeString(st_Name);
dest.writeString(st_Class);
}

包裹创建者:

public static final Parcelable.Creator<Track> CREATOR
= new Parcelable.Creator<Track>() {
public Student createFromParcel(Parcel in) {
return new Track(in);
}

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

构造函数:

public Student (Parcel source){
/*
* Reconstruct from the Parcel. Keep same order as in writeToParcel()
*/
st_AcadimicNumber = source.readString();
st_Name = source.readString();
st_Class = source.readString();
}

完成了。

问候

关于android - 将对象从一个 Activity 传递到另一个 Activity 时出错(使用 Parcelable),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13787497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com