android – 将对象从一个活动传递给另一个时出错(使用Parcelable)

前端之家收集整理的这篇文章主要介绍了android – 将对象从一个活动传递给另一个时出错(使用Parcelable)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用Parcelable将对象类从一个活动传递给另一个

我创建了一个班级,并将其命名为学生

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

    }

}

在主要活动中我使用此代码传递Obejct …

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

在第二个活动(检索一个)

我使用此代码(错误在这里)

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

它已经完成了.

问候

原文链接:https://www.f2er.com/android/308885.html

猜你在找的Android相关文章