Error While Passing An Object From An Activity To

2019-02-22 04:11发布

I Tried To Pass An Object Class From An Activity To Another Using Parcelable

I've Create A Class And Name it 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

    }

}

In The Main Activity I Use This Code To Pass The Obejct ...

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

In The Second Activity (Retrieve One)

I use This Code (ERROR IS HERE)

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

LogCat Error

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

1条回答
男人必须洒脱
2楼-- · 2019-02-22 04:53

You need to implement the code to write and read your class fields from parcel.

In writeToParcel:

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

The Parcel CREATOR:

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];
    }
};

And the constructor:

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();
}   

And it's done.

Regards

查看更多
登录 后发表回答