制作一个包含自定义对象列表的可分割类

发布于 2024-12-12 07:19:03 字数 5243 浏览 0 评论 0原文

我在使列表对象可解析时遇到错误(我认为读取对象时发生错误) 这是我的代码

    public class TestSample implements Parcelable {
    int intValue;
    String stirngValue;
    private List<DocumentControlPolicy> 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<TestSample> CREATOR
                   = new Parcelable.Creator<TestSample>() {
           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.<init>(TestSample.java:41)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.zenprise.android.policy.TestSample.<init>(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

I am getting error while making a list object parsable(i think the error occured while reading the object)
here is my code

    public class TestSample implements Parcelable {
    int intValue;
    String stirngValue;
    private List<DocumentControlPolicy> 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<TestSample> CREATOR
                   = new Parcelable.Creator<TestSample>() {
           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;
    }

}

And here is the code to send the data from one activity to other

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

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

here is the code to get the data

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

But i get the following exception

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.<init>(TestSample.java:41)
10-28 19:30:58.281: ERROR/AndroidRuntime(3371):     at com.zenprise.android.policy.TestSample.<init>(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2024-12-19 07:19:03

在使用对象cpls之前,您尚未创建它
在您的 TestSample(Parcel in) 构造函数中,您正在使用 cpls 而不声明它..
你应该这样做

 private TestSample(Parcel in) {
        intValue = in.readInt();
        stirngValue=in.readString();
        cpls = new ArrayList<DocumentControlPolicy>();//The code you missed.. 
        in.readTypedList(cpls,DocumentControlPolicy.CREATOR);
    }

You have not created the object cpls before you use it
In your TestSample(Parcel in) constructor, you are using cpls without declaring it..
You should do like this

 private TestSample(Parcel in) {
        intValue = in.readInt();
        stirngValue=in.readString();
        cpls = new ArrayList<DocumentControlPolicy>();//The code you missed.. 
        in.readTypedList(cpls,DocumentControlPolicy.CREATOR);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文