1

これが私の小包への書き込みコードです

public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(poiDescription);
        dest.writeString(latitude);
        dest.writeString(longitude);
        dest.writeString(placeName);
        dest.writeString(plistPath);
        dest.writeString(poiDirName);
            if (null != isMascotPresent)
                dest.writeString(isMascotPresent);
            if (null != startMascottTime)
                dest.writeInt(startMascottTime);
            if (null != mascottDuration)
                dest.writeInt(mascottDuration);
}

public PointOfInterest(Parcel source) { 
        poiDescription = source.readString();
        latitude = source.readString();
        longitude = source.readString();
        placeName = source.readString();
        plistPath = source.readString();
        poiDirName = source.readString();
        audioLinkDownload = source.readString();
        audioLinkStream = source.readString();
        poiName = source.readString();
        poiIndex = source.readInt();
        poiPaused = source.readString();
        source.readList(imageList, null);
        source.readList(durationList, null);
        if (null != isMascotPresent)
            isMascotPresent = source.readString();
        if (null != startMascottTime)
               startMascottTime=source.readInt();
        if (null != mascottDuration)
                  mascottDuration=source.readInt();
}


This is how I am reading the values
listOfPOI = getIntent().getParcelableArrayListExtra("poi");

このタラは、上のマスコットがなくても問題なく動作します。

しかし、マスコットに関する3行を追加すると、アプリケーションがクラッシュします。私はこれに頭を悩ませています、なぜこれが起こっているのかわかりません、誰かが私に問題を知らせてもらえますか?

実行時の例外が発生し、パーセラブルのアンマーシャリングエラーがあるという問題が発生します

これは私が得ている例外です

 ERROR/AndroidRuntime(2061): FATAL EXCEPTION: main
 ERROR/AndroidRuntime(2061): java.lang.RuntimeException: Unable to start activity       ComponentInfo{com.Invenger/com.Invenger.Player.Audio}: java.lang.RuntimeException: Parcel android.os.Parcel@40570848: Unmarshalling unknown type code 7536748 at offset 1064
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 ERROR/AndroidRuntime(2061):     at android.os.Handler.dispatchMessage(Handler.java:99)
 ERROR/AndroidRuntime(2061):     at android.os.Looper.loop(Looper.java:130)
 ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.main(ActivityThread.java:3683)
 ERROR/AndroidRuntime(2061):     at java.lang.reflect.Method.invokeNative(Native Method)
 ERROR/AndroidRuntime(2061):     at java.lang.reflect.Method.invoke(Method.java:507)
 ERROR/AndroidRuntime(2061):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
ERROR/AndroidRuntime(2061):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 ERROR/AndroidRuntime(2061):     at dalvik.system.NativeStart.main(Native Method)
 ERROR/AndroidRuntime(2061): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@40570848: Unmarshalling unknown type code 7536748 at offset 1064
 ERROR/AndroidRuntime(2061):     at android.os.Parcel.readValue(Parcel.java:1913)
 ERROR/AndroidRuntime(2061):     at android.os.Parcel.readListInternal(Parcel.java:2092)
 ERROR/AndroidRuntime(2061):     at android.os.Parcel.readArrayList(Parcel.java:1536)
 ERROR/AndroidRuntime(2061):     at android.os.Parcel.readValue(Parcel.java:1867)
ERROR/AndroidRuntime(2061):     at android.os.Parcel.readMapInternal(Parcel.java:2083)
ERROR/AndroidRuntime(2061):     at android.os.Bundle.unparcel(Bundle.java:208)
ERROR/AndroidRuntime(2061):     at   android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
 ERROR/AndroidRuntime(2061):     at android.content.Intent.getParcelableArrayListExtra(Intent.java:3448)
    ERROR/AndroidRuntime(2061):     at com.Invenger.Player.Audio.onCreate(Audio.java:162)
    ERROR/AndroidRuntime(2061):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
   ERROR/AndroidRuntime(2061):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
   ERROR/AndroidRuntime(2061):     ... 11 more
4

3 に答える 3

12

isMascotPresent初期化していない限りstartMascottTimemascottDurationどこかで常にnullコンストラクターに含まれることになりますPointOfInterest(Parcel source)。したがって、オブジェクトをパーセルするときに値を書き込んだとしても、それらは読み取られず、次の値は正しくありません。

nullマスコット情報を読み書きするときにそれらが正しいかどうかを確認する代わりに、 boolean:を使用できます。

    if (null != isMascotPresent) {
        dest.writeByte((byte) 1);
        dest.writeString(isMascotPresent);
        dest.writeInt(startMascottTime);
        dest.writeInt(mascottDuration);
    } else {
        dest.writeByte((byte) 0);
    }

そしてそれを読み返すには:

    if(source.readByte() == 1) {
        isMascotPresent = source.readString();
        startMascottTime = source.readInt();
        mascottDuration = source.readInt();
    } else {
        isMascotPresent = null;
        startMascottTime = null;
        mascottDuration = null;
    }

また、小包に書き出すよりも多くの値を読み込んでいます。値は同じ順序で読み書きする必要があります。

于 2011-09-22T09:56:29.260 に答える
0

Android Parcelable ボイラープレート コードを生成する android studio プラグインを使用できます。[ファイル] -> [設定] -> [プラグイン] -> [リポジトリの参照] -> [Android Parcelable コード ジェネレーター] を検索します。または https://plugins.jetbrains.com/plugin/7332?pr=

問題は、オブジェクトの書き込みと読み取りの順序にあります...ファイルの読み取りと同様に、順序は同じでなければなりません。

于 2016-04-22T08:33:23.910 に答える
-3

なんとか問題を解決しました。パーセル可能にする代わりに、静的にしました。これは、静的にする標準的な方法ではないかもしれません。ただ、荷物のサイズに制限があるように感じます。15 個以上の変数を追加すると、機能しません。だからそれは今私のために働いています。

より良い答えは受け入れられます

于 2011-09-18T07:01:38.920 に答える