getParcelableArrayListExtra返回nullpointerexception

时间:2012-01-17 22:04:42

标签: android nullpointerexception parcelable

我遇到了getParcelableArrayListExtra和Null Pointer Exception的问题。

工作

我的活动:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fetch = new ArrayList<Custom>();
    generateEntries();

    Log.i("fetch", fetch.toString());

    Intent myIntent = new Intent(this, CustomObject.class);
    //myIntent.putParcelableArrayListExtra("my", fetch);
    myIntent.putExtra("my", "name");
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(myIntent);
}

CustomObject:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customobject);
    lv = (ListView) findViewById(R.id.listview);

    recievedList = new ArrayList<Custom>();

    in = getIntent();

    String s = bu.getString("my");

    Log.i("s", "s");
}

不工作

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fetch = new ArrayList<Custom>();
    generateEntries();

    Log.i("fetch", fetch.toString());

    Intent myIntent = new Intent(this, CustomObject.class);
    myIntent.putParcelableArrayListExtra("my", fetch);
    //myIntent.putExtra("my", "name");
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(myIntent);
}

CustomObject:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customobject);
    lv = (ListView) findViewById(R.id.listview);

    recievedList = new ArrayList<Custom>();

    in = getIntent();

    recievedList = in.getParcelableArrayListExtra("my"); // NULL POINTER EXCEPTION
}

ArrayList有什么问题?

有人帮帮我吗?

............................................... .................................................. ....

public class Custom implements Parcelable {

    private String alarmTitle;
    private String alarmType;
    private String alarmTime;
    private String alarmDate;
    private List<String> shortVakatName;
    private List<String> vakatActive;

    public Custom(String entry1, List<String> list1, List<String> list2, String entry3, String entry4, String entry5){

        this.shortVakatName = new ArrayList<String>();
        this.vakatActive = new ArrayList<String>();

        this.alarmTitle = entry1;
        this.shortVakatName = list1;
        this.vakatActive = list2;
        this.alarmType = entry3;
        this.alarmTime = entry4;
        this.alarmDate = entry5;
    }

    private Custom(Parcel in){
        alarmTitle = in.readString();
        in.readStringList(shortVakatName);
        in.readStringList(vakatActive);
        alarmTime = in.readString();
        alarmDate = in.readString();
    }

    public static final Parcelable.Creator<Custom> CREATOR =
            new Parcelable.Creator<Custom>() {

        public Custom createFromParcel(Parcel source) {
            return new Custom(source);
        }

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

    };

    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(alarmTitle);
        dest.writeStringList(shortVakatName);
        dest.writeStringList(vakatActive);
        dest.writeString(alarmType);
        dest.writeString(alarmTime);
        dest.writeString(alarmDate);
    }
}

1 个答案:

答案 0 :(得分:16)

当您打开包裹以创建新对象时,问题就出现了。特别是,您拨打readStringList()的电话。此方法旨在使用宗地中的数据填充现有对象,而不是创建新对象。

实现当解包parcel时,根据Parcel的定义调用以Parcelable.CREATOR作为参数的构造函数,而不是其他参数化构造函数。因此,shortVakatNamevakatActive都没有被初始化为任何东西(它们是空指针)。

您可以通过执行以下两项操作中的一项来解决问题,或者让包裹在膨胀数据时为您创建List

private Custom(Parcel in){
    alarmTitle = in.readString();
    shortVakatName = in.createStringArrayList();
    vakatActive = in.createStringArrayList();
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}

或者,在告诉Parcel填充数据之前创建对象。

private Custom(Parcel in){
    shortVakatName = new ArrayList<String>();
    vakatActive = new ArrayList<String>();

    alarmTitle = in.readString();
    in.readStringList(shortVakatName);
    in.readStringList(vakatActive);
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}

另请注意,在这两个示例中,我修改了阅读顺序以匹配您的writeToParcel()方法(您错过了alarmType参数,当您通过数据传递数据时,这会导致奇怪的结果Intent

HTH