Зрабіць клас parcelable, які змяшчае спіс карыстацкіх аб'ектаў

Я атрымліваю памылку, робячы спіс аб'ектаў аформленай (я думаю, што адбылася памылка пры чытанні аб'екта) Вось мой код

    public class TestSample implements Parcelable {
    int intValue;
    String stirngValue;
    private List cpls;
    @Override
    public int describeContents() {
       //TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(intValue);
        dest.writeString(stirngValue);
        dest.writeTypedList(cpls);
        dest.writeList(cpls);
    }
    public static final Parcelable.Creator CREATOR
                   = new Parcelable.Creator() {
           public TestSample createFromParcel(Parcel in) {
               return new TestSample(in);
           }

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

    private TestSample(Parcel in) {
        intValue = in.readInt();
        stirngValue=in.readString();
        in.readTypedList(cpls,DocumentControlPolicy.CREATOR);
    }
    public TestSample(int intValue, String stirngValue) {
        super();
        this.intValue = intValue;
        this.stirngValue = stirngValue;
    }

}

А вось код для перадачы дадзеных ад аднаго віду дзейнасці на іншы

Intent nextpage = new Intent(this, Secondpage.class);

        TestSample tst= new TestSample(1,"Tood");
        Log.i("myTag", tst.toString());
        nextpage.putExtra("ONE", tst);
        startActivity(nextpage);

Вось код, каб атрымаць дадзеныя

 TestSample tst = getIntent().getParcelableExtra("ONE");
        if(tst != null){
            Log.i("myTag", "second "+tst.toString());
        }else{
            Log.i("myTag","second tst is null");
        }

Але я атрымліваю наступнае выключэнне

10-28 19:30:58.281: ERROR/AndroidRuntime(3371): FATAL EXCEPTION: main
10-28 19:30:58.281: ERROR/AndroidRuntime(3371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.parceblecheck/com.app.parceblecheck.Secondpage}: java.lang.NullPointerException
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.os.Looper.loop(Looper.java:123)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at java.lang.reflect.Method.invoke(Method.java:521)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at dalvik.system.NativeStart.main(Native Method)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371): Caused by: java.lang.NullPointerException
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.os.Parcel.readTypedList(Parcel.java:1555)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.zenprise.android.policy.TestSample.(TestSample.java:41)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.zenprise.android.policy.TestSample.(TestSample.java:38)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.zenprise.android.policy.TestSample$1.createFromParcel(TestSample.java:30)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.zenprise.android.policy.TestSample$1.createFromParcel(TestSample.java:1)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.os.Parcel.readParcelable(Parcel.java:1906)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.os.Parcel.readValue(Parcel.java:1771)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.os.Parcel.readMapInternal(Parcel.java:2008)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.os.Bundle.unparcel(Bundle.java:208)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.os.Bundle.getParcelable(Bundle.java:1100)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.content.Intent.getParcelableExtra(Intent.java:3396)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.app.parceblecheck.Secondpage.onCreate(Secondpage.java:27)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     ... 11 more
4

1 адказы

Вы не стварылі аб'ект CPLS , перш чым выкарыстоўваць яго
У вашым TestSample (Parcel в) канструктар, вы карыстаецеся CPLS без абвяшчэння ..
Вы павінны зрабіць так

 private TestSample(Parcel in) {
        intValue = in.readInt();
        stirngValue=in.readString();
        cpls = new ArrayList();//The code you missed.. 
        in.readTypedList(cpls,DocumentControlPolicy.CREATOR);
    }
10
дададзена
Трэба ініцыялізаваць яго ў канструктару?
дададзена аўтар Kaveesh Kanwal, крыніца