2

Parcelable を拡張するクラスがあります。Parcelable を拡張する別のクラスの ArrayList であるメンバー変数があります。

Intent 内の MyClass1 オブジェクトを Activity に渡し、それを読み取ります。コンストラクターが in.readTypedList に到達すると、アプリは次のエラーでクラッシュします。

04-11 14:19:11.223: E/AndroidRuntime(502): Uncaught handler: thread main exiting due to uncaught exception
04-11 14:19:11.234: E/AndroidRuntime(502): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.mypackage.app/com.mypackage.app.activity.MyActivity}: java.lang.NullPointerException
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3329)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3371)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.app.ActivityThread.access$2700(ActivityThread.java:119)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1893)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Looper.loop(Looper.java:123)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-11 14:19:11.234: E/AndroidRuntime(502):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 14:19:11.234: E/AndroidRuntime(502):  at java.lang.reflect.Method.invoke(Method.java:521)
04-11 14:19:11.234: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-11 14:19:11.234: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-11 14:19:11.234: E/AndroidRuntime(502):  at dalvik.system.NativeStart.main(Native Method)
04-11 14:19:11.234: E/AndroidRuntime(502): Caused by: java.lang.NullPointerException
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Parcel.readTypedList(Parcel.java:1497)
04-11 14:19:11.234: E/AndroidRuntime(502):  at com.mypackage.app.utils.Strength.<init>(Strength.java:44)
04-11 14:19:11.234: E/AndroidRuntime(502):  at com.mypackage.app.utils.Strength$1.createFromParcel(Strength.java:171)
04-11 14:19:11.234: E/AndroidRuntime(502):  at com.mypackage.app.utils.Strength$1.createFromParcel(Strength.java:1)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Parcel.readParcelable(Parcel.java:1845)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Parcel.readValue(Parcel.java:1713)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Parcel.readMapInternal(Parcel.java:1947)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Bundle.unparcel(Bundle.java:169)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Bundle.containsKey(Bundle.java:210)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.content.Intent.hasExtra(Intent.java:3067)
04-11 14:19:11.234: E/AndroidRuntime(502):  at com.mypackage.app.activity.MyActivity.onActivityResult(MyActivity.java:306)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.app.Activity.dispatchActivityResult(Activity.java:3828)
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3325)
04-11 14:19:11.234: E/AndroidRuntime(502):  ... 11 more  

public class MyClass1 implements Parcelable {

    private int mVar1;
    private int mVar2;
    private int mVar3;
    private long mVar5;
    private long mVar6;
    private String mVar7;
    private String mVar8;
    private String mVar9;
    private String mVar10;
    private ArrayList<MyClass2> mVar11;

    public MyClass1() {
        mVar1 = -1;
        mVar2 = -1;
        mVar3 = -1;
        mVar5 = 0;
        mVar6 = 0;
        mVar7 = null;
        mVar8 = null;
        mVar9 = null;
        mVar10 = null;
        mVar11 = new ArrayList<MyClass2>();
    }

    public MyClass1(Parcel in) {
        mVar1 = in.readInt();
        mVar2 = in.readInt();
        mVar3 = in.readInt();
        mVar5 = in.readLong();
        mVar6 = in.readLong();
        mVar8 = in.readString();
        mVar9 = in.readString();
        mVar10 = in.readString();
        in.readTypedList(mVar11, MyClass2.CREATOR);
        mVar7 = in.readString();
    }

    public int getVar1() {
        return mVar1;
    }

    public void setVar1(int intVar1) {
        this.mVar1 = intVar1;
    }

    public int getVar2() {
        return mVar2;
    }

    public void setVar2(int intVar2) {
        this.mVar2 = intVar2;
    }

    public int getVar3() {
        return mVar3;
    }

    public void setVar3(int intVar3) {
        this.mVar3 = intVar3;
    }

    public long getVar5() {
        return mVar5;
    }

    public void setVar5(long lngVar5) {
        this.mVar5 = lngVar5;
    }

    public long getVar6() {
        return mVar6;
    }

    public void setVar6(long lngVar6) {
        this.mVar6 = lngVar6;
    }

    public String getVar7() {
        return mVar7;
    }

    public void setVar7(String strVar7) {
        this.mVar7 = strVar7;
    }

    public String getVar8() {
        return mVar8;
    }

    public void setVar8(String strVar8) {
        this.mVar8 = strVar8;
    }

    public String getVar9() {
        return mVar9;
    }

    public void setVar9(String strVar9) {
        this.mVar9 = strVar9;
    }

    public String getVar10() {
        return mVar10;
    }

    public void setVar10(String strVar10) {
        this.mVar10 = strVar10;
    }

    public ArrayList<MyClass2> getVar11() {
        return mVar11;
    }

    public void setVar11(ArrayList<MyClass2> aryVar11) {
        this.mVar11 = aryVar11;
    }

    public void addVar11(MyClass2 mc2) {
        this.mVar11.add(mc2);
    }

    public MyClass2 removeVar11(int index) {
        if (index < this.mVar11.size()) {
            return this.mVar11.remove(index);
        }

        return null;
    }

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

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

    public int describeContents() {
        return 0;
    }

    public void writeToParcel (Parcel dest, int flags) {
        dest.writeInt(mVar1);
        dest.writeInt(mVar2);
        dest.writeInt(mVar3);
        dest.writeLong(mVar5);
        dest.writeLong(mVar6);
        dest.writeString(mVar8);
        dest.writeString(mVar9);
        dest.writeString(mVar10);
        if (mVar11 == null) {
            mVar11 = new ArrayList<MyClass2>();
        }
        dest.writeTypedList(mVar11);
        dest.writeString(mVar7);
    }
}






public class MyClass2 implements Parcelable {

    private int mVar1;
    private int mVar2;
    private String mVar3;
    private String mVar4;

    public MyClass2() {
        mVar1 = -1;
        mVar2 = -1;
        mVar3 = null;
        mVar4 = null;
    }

    public MyClass2(Parcel in) {
        mVar1 = in.readInt();
        mVar2 = in.readInt();
        mVar3 = in.readString();
        mVar4 = in.readString();
    }

    public int getVar1() {
        return mVar1;
    }

    public void setVar1(int intVar1) {
        this.mVar1 = intVar1;
    }

    public int getVar2() {
        return mVar2;
    }

    public void setVar2(int intVar2) {
        this.mVar2 = intVar2;
    }

    public String getVar3() {
        return mVar3;
    }

    public void setVar3(String strVar3) {
        this.mVar3 = strVar3;
    }

    public String getVar4() {
        return mVar4;
    }

    public void setVar4(String strVar4) {
        this.mVar4 = strVar4;
    }

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

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

    public int describeContents() {
        return 0;
    }

    public void writeToParcel (Parcel dest, int flags) {
        dest.writeInt(mVar1);
        dest.writeInt(mVar2);
        dest.writeString(mVar3);
        dest.writeString(mVar4);
    }
}

私はこれをくまなく調べましたが、何も見えません.

アイデア?

4

1 に答える 1

11
04-11 14:19:11.234: E/AndroidRuntime(502): Caused by: java.lang.NullPointerException
04-11 14:19:11.234: E/AndroidRuntime(502):  at android.os.Parcel.readTypedList(Parcel.java:1497)

ソースコードを見てください ( Parcel.java:1497 ):

public final <T> void readTypedList(List<T> list, Parcelable.Creator<T> c) {
  int M = list.size();  // line 1497
  ... ...

ArrayList<MyClass2> mVar11 は、Parcel.readTypedList() を呼び出す前に正しく初期化されていません。

public MyClass1(Parcel in) {
  mVar1 = in.readInt();
  mVar2 = in.readInt();
  mVar3 = in.readInt();
  mVar5 = in.readLong();
  mVar6 = in.readLong();
  mVar8 = in.readString();
  mVar9 = in.readString();
  mVar10 = in.readString();
  mVar11 = new ArrayList<MyClass2>();
  in.readTypedList(mVar11, MyClass2.CREATOR);
  mVar7 = in.readString();
}

これにより、NullPointerException エラーが修正されます。

于 2012-04-11T21:39:22.387 に答える