自定义Arraylist不会显示所有元素,但是在使用Parcelable

时间:2019-03-03 12:13:22

标签: arraylist parcelable

我正在尝试使用Parcelable将自定义Pojo的数组列表从一个Activity传递到另一个Activity。我还使Pojo类实现了Parcelable。在活动1中,arraylist包含18个元素。当传递给活动2时,Arraylist的大小显示为18,但是当我尝试打印元素时,仅打印了1个元素,此后,我得到了空指针异常。这就是我从活动1中传递arraylist的方式:

public static final ArrayList<CategoryQuestionPojo> mCatQue = new ArrayList<>();

intent = new Intent(this, Gender.class);
bundle = new Bundle();

bundle.putParcelableArrayList("mCatQuePojo", mCatQue);
intent.putExtras(bundle);
startActivity(intent);

在上面的代码中,lv_catQue的大小为18。在活动2中,onCreate方法中,我接受的arraylist为:

ArrayList<CategoryQuestionPojo> mCatQuePojo = new ArrayList<>();
mCatQuePojo = this.getIntent().getExtras().getParcelableArrayList("mCatQuePojo");
Log.e("Array size: ", String.valueOf(mCatQuePojo.size()));
for (int i = 0; i <= mCatQuePojo.size()-1; i++){
        Log.e("index : sequence no. ", ((i)+" : "+mCatQuePojo.get(i).getLv_seqNo()));
        Log.e("mCatue array:", mCatQuePojo.get(i).getLv_queId());
    }

在“ For”循环中,打印了第一个元素,但是此后,我得到了空指针异常:

E/Array size:: 18
E/index : sequence no.: 0 : 1
E/mCatue array:: GENDER
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.events.jashna.jashnaeventandroid, PID: 11335
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.events.jashna.jashnaeventandroid/com.events.jashna.jashnaeventandroid.Activity.Questionnaire.Gender}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.events.jashna.jashnaeventandroid.Pojos.CategoryQuestionPojo.getLv_seqNo()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6635)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.events.jashna.jashnaeventandroid.Pojos.CategoryQuestionPojo.getLv_seqNo()' on a null object reference
    at com.events.jashna.jashnaeventandroid.Activity.Questionnaire.Gender.onCreate(Gender.java:54)
    at android.app.Activity.performCreate(Activity.java:7084)
    at android.app.Activity.performCreate(Activity.java:7075)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:176) 
    at android.app.ActivityThread.main(ActivityThread.java:6635) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
I/Process: Sending signal. PID: 11335 SIG: 9
Application terminated.

有人可以告诉我我做错了什么吗?谢谢你的帮助。 我还在活动2上尝试了以下代码,但得到了相同的结果。

 Bundle bundle = getIntent().getExtras();
 mCatQuePojo = bundle.getParcelableArrayList("mCatQuePojo");

下面是我的Pojo课堂:

public class CategoryQuestionPojo implements Parcelable {

private String lv_categoryId;
private String lv_queId;
private QuestionnairePojo lv_questionnaire;
private int lv_seqNo;
private String lv_tableName;
private List<GuestRangePojo> lv_guestRange;
private List<BudgetRangePojo> lv_budgetRange;
private int lv_lowerLimit;
private int lv_upperLimit;
private String lv_noRange;
private VenueCategoryPojo lv_venueCat;
private ServiceMasterPojo lv_serviceMaster;


public CategoryQuestionPojo() {

}
public CategoryQuestionPojo(String lv_categoryId, String lv_queId, int lv_seqNo, String lv_tableName,
                             int lv_lowerLimit, int lv_upperLimit, String lv_noRange) {
    super();
    this.lv_categoryId = lv_categoryId;
    this.lv_queId = lv_queId;
    this.lv_seqNo = lv_seqNo;
    this.lv_tableName = lv_tableName;
    this.lv_lowerLimit = lv_lowerLimit;
    this.lv_upperLimit = lv_upperLimit;
    this.lv_noRange = lv_noRange;
}


public CategoryQuestionPojo(String lv_categoryId, String lv_queId, QuestionnairePojo lv_questionnaire,
                             int lv_seqNo, String lv_tableName, List<GuestRangePojo> lv_guestRange,
                             List<BudgetRangePojo> lv_budgetRange, int lv_lowerLimit, int lv_upperLimit, String lv_noRange,
                             VenueCategoryPojo lv_venueCat, ServiceMasterPojo lv_serviceMaster)
{
    super();
    this.lv_categoryId = lv_categoryId;
    this.lv_queId = lv_queId;
    this.lv_questionnaire = lv_questionnaire;
    this.lv_seqNo = lv_seqNo;
    this.lv_tableName = lv_tableName;
    this.lv_guestRange = lv_guestRange;
    this.lv_budgetRange = lv_budgetRange;
    this.lv_lowerLimit = lv_lowerLimit;
    this.lv_upperLimit = lv_upperLimit;
    this.lv_noRange = lv_noRange;
    this.lv_venueCat = lv_venueCat;
    this.lv_serviceMaster = lv_serviceMaster;
}



public String getLv_categoryId() {
    return lv_categoryId;
}
public void setLv_categoryId(String lv_categoryId) {
    this.lv_categoryId = lv_categoryId;
}
public String getLv_queId() {
    return lv_queId;
}
public void setLv_queId(String lv_queId) {
    this.lv_queId = lv_queId;
}
public int getLv_seqNo() {
    return lv_seqNo;
}
public void setLv_seqNo(int lv_seqNo) {
    this.lv_seqNo = lv_seqNo;
}
public String getLv_tableName() {
    return lv_tableName;
}
public void setLv_tableName(String lv_tableName) {
    this.lv_tableName = lv_tableName;
}
public int getLv_lowerLimit() {
    return lv_lowerLimit;
}
public void setLv_lowerLimit(int i) {
    this.lv_lowerLimit = i;
}
public int getLv_upperLimit() {
    return lv_upperLimit;
}
public void setLv_upperLimit(int lv_upperLimit) {
    this.lv_upperLimit = lv_upperLimit;
}
public String getLv_noRange() {
    return lv_noRange;
}
public void setLv_noRange(String lv_noRange) {
    this.lv_noRange = lv_noRange;
}
public QuestionnairePojo getLv_questionnaire() {
    return lv_questionnaire;
}
public void setLv_questionnaire(QuestionnairePojo lv_questionnaire) {
    this.lv_questionnaire = lv_questionnaire;
}
public List<GuestRangePojo> getLv_guestRange() {
    return lv_guestRange;    }
public void setLv_guestRange(List<GuestRangePojo> lv_guestRange) {
    this.lv_guestRange = lv_guestRange;
}
public List<BudgetRangePojo> getLv_budgetRange() {
    return lv_budgetRange;
}    public void setLv_budgetRange(List<BudgetRangePojo> lv_budgetRange) {
   this.lv_budgetRange = lv_budgetRange;
}

public VenueCategoryPojo getLv_venueCat() {
    return lv_venueCat;
}
public void setLv_venueCat(VenueCategoryPojo lv_venueCat) {
    this.lv_venueCat = lv_venueCat;
}
public ServiceMasterPojo getLv_serviceMaster() {
    return lv_serviceMaster;
}
public void setLv_serviceMaster(ServiceMasterPojo lv_serviceMaster) {
    this.lv_serviceMaster = lv_serviceMaster;
}


@Override
public int describeContents() {
    return 0;
}

public CategoryQuestionPojo(Parcel in){
    lv_questionnaire = (QuestionnairePojo) in.readParcelable(QuestionnairePojo.class.getClassLoader());
    lv_venueCat = (VenueCategoryPojo) in.readParcelable(VenueCategoryPojo.class.getClassLoader());
    lv_serviceMaster = (ServiceMasterPojo) in.readParcelable(ServiceMasterPojo.class.getClassLoader());
    lv_categoryId = in.readString();
    lv_queId = in.readString();

    lv_seqNo = in.readInt();
    lv_tableName = in.readString();
    lv_budgetRange = in.createTypedArrayList(BudgetRangePojo.CREATOR);
    lv_guestRange = in.createTypedArrayList(GuestRangePojo.CREATOR);
    lv_lowerLimit = in.readInt();
    lv_upperLimit = in.readInt();
    lv_noRange = in.readString();

}


@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeParcelable(lv_questionnaire, flags);
    dest.writeParcelable(lv_venueCat, flags);
    dest.writeParcelable(lv_serviceMaster, flags);
    dest.writeString(lv_categoryId);
    dest.writeString(lv_queId);
    dest.writeLong(lv_seqNo);
    dest.writeString(lv_tableName);
    dest.writeTypedList(lv_budgetRange);
    dest.writeTypedList(lv_guestRange);
    dest.writeInt(lv_lowerLimit);
    dest.writeInt(lv_upperLimit);
    dest.writeString(lv_noRange);

}

public static final Creator<CategoryQuestionPojo> CREATOR = new Creator<CategoryQuestionPojo>() {
    @Override
    public CategoryQuestionPojo createFromParcel(Parcel in) {
        return new CategoryQuestionPojo(in);
    }

    @Override
    public CategoryQuestionPojo[] newArray(int size) {
        return new CategoryQuestionPojo[size];
    }
};
}

0 个答案:

没有答案