自定义对象列表和Parcelable给出NullPointerException

时间:2015-06-09 19:34:43

标签: java android nullpointerexception parcelable

我正在尝试理解Android中Parcelable的原理,我写这个简单的代码来查找当我尝试" parcel"自定义对象列表 - 它给出了我的NullPointerException

这是我的代码:

package cz.united121.android.testpupose.Objects.HelperObject;

import android.os.Parcel;
import android.os.Parcelable;

public class MyString implements Parcelable {
private static final String TAG = MyString.class.getName();


String mString;

public MyString(){

}

public MyString(String mString){
    this.mString = mString;
}

public MyString(Parcel in){
    mString = in.readString();
}

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

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

public static final Parcelable.Creator<MyString> CREATOR
        = new Parcelable.Creator<MyString>() {
    public MyString createFromParcel(Parcel in) {
        return new MyString(in);
    }

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

}

第二类有MyString列表

package cz.united121.android.testpupose.Objects;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cz.united121.android.testpupose.Objects.HelperObject.MyString;
public class CustomSmall implements Parcelable {
private static final String TAG = CustomSmall.class.getName();


int mId;
String mName;
int mDummy;
List<MyString> mStringList;

public CustomSmall(){
    Log.d(TAG,"CustomSmall()");
    mStringList = new ArrayList<>();
}

public CustomSmall(Parcel in){
    Log.d(TAG,"CustomSmall(Parcel in)");
    mId = in.readInt();
    mName = in.readString();
    mDummy = in.readInt();
    in.readTypedList(mStringList,MyString.CREATOR);
}

public CustomSmall(int mId, String mName, int mDummy, List<MyString> mStringList){
    Log.d(TAG, "CustomSmall(int mId, String mName, int mDummy, List<MyString> mStringList)");
    this.mId = mId;
    this.mName = mName;
    this.mDummy = mDummy;
    this.mStringList = mStringList;
}

@Override
public int describeContents() {
    Log.d(TAG,"describeContents()");
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    Log.d(TAG,"writeToParcel(Parcel dest, int flags)");
    dest.writeInt(mId);
    dest.writeString(mName);
    dest.writeInt(mDummy);
    dest.writeTypedList(mStringList);
}

public static final Parcelable.Creator<CustomSmall> CREATOR
        = new Parcelable.Creator<CustomSmall>() {
    public CustomSmall createFromParcel(Parcel in) {
        Log.d(TAG,"createFromParcel(Parcel in)");
        return new CustomSmall(in);
    }

    public CustomSmall[] newArray(int size) {
        Log.d(TAG,"newArray(int size)");
        return new CustomSmall[size];
    }
};
}

以下是我测试的方法 点击后有一项活动:

    MyString myString1 = new MyString("a");
    MyString myString2 = new MyString("b");
    MyString myString3 = new MyString("c");
    MyString myString4 = new MyString("d");

    List<MyString> myStringList1 = new ArrayList<>();
    myStringList1.add(myString1);
    myStringList1.add(myString4);

    List<MyString> myStringList2 = new ArrayList<>();
    myStringList1.add(myString1);
    myStringList1.add(myString2);
    myStringList1.add(myString3);
    myStringList1.add(myString4);

    CustomSmall customSmall_1 = new CustomSmall(1,"First",42,myStringList1);
    CustomSmall customSmall_2 = new CustomSmall(2,"Second",88,myStringList2);

    bundle.putParcelable(CUSTOM_SMALL + "1",customSmall_1);
    bundle.putParcelable(CUSTOM_SMALL + "2",customSmall_2);
    Bundle b = new Bundle();
    Intent toNavigationDrawer = new Intent(MainActivity.this,NavigationDrawerTest.class);
    toNavigationDrawer.putExtras(b);
    startActivity(toNavigationDrawer);

和类NavigationDrawer有这个:

    Bundle bundle = getIntent().getExtras();
    CustomSmall customSmall_1 = bundle.getParcelable(CUSTOM_SMALL + "1");
    CustomSmall customSmall_2 = bundle.getParcelable(CUSTOM_SMALL + "2");

但是我给出了错误:

Unable to start activity ComponentInfo{cz.united121.android.testpupose/cz.united121.android.testpupose.NavigationDrawerTest}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
...
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
...
at cz.united121.android.testpupose.Objects.CustomSmall.<init>(CustomSmall.java:34)
        at cz.united121.android.testpupose.Objects.CustomSmall$1.createFromParcel(CustomSmall.java:64)
        at cz.united121.android.testpupose.Objects.CustomSmall$1.createFromParcel(CustomSmall.java:61)

和CustomSmall.java:34是行:

 in.readTypedList(mStringList,MyString.CREATOR); in public CustomSmall(Parcel in){

有人知道解决方案吗?我差不多2天试图解决这个问题。非常感谢提前

1 个答案:

答案 0 :(得分:5)

您没有在构造函数中初始化您的mStringList列表:

public CustomSmall(Parcel in){
    Log.d(TAG,"CustomSmall(Parcel in)");
    mId = in.readInt();
    mName = in.readString();
    mDummy = in.readInt();
    mStringList = new ArrayList<>();
    in.readTypedList(mStringList,MyString.CREATOR);
}