【问题标题】:List of custom object and Parcelable give NullPointerException自定义对象列表和 Parcelable 给出 NullPointerException
【发布时间】:2015-06-09 19:34:42
【问题描述】:

我正在尝试了解 Android 中 Parcelable 的原理,我编写了这个简单的代码来查找当我尝试“打包”自定义对象列表时发生了什么 - 它给出了我的 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 天。非常感谢提前

【问题讨论】:

    标签: java android nullpointerexception parcelable


    【解决方案1】:

    你没有在构造函数中初始化你的 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);
    }
    

    【讨论】:

    • 非常感谢 :D 尴尬,但我一直认为没有参数的构造函数是先调用然后这个构造函数(所以我没有“再次”初始化字符串 arrayList :D)
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多