AdMob插页式onSaveInstanceState和Serializables Parcelable遇到IOException编写可序列化对象

时间:2014-07-14 22:09:39

标签: java android serialization admob interstitial

我将一个Serializable数组保存在片段onSaveInstanceState中。这导致了常规片段破坏这种方向变化的问题。但是,当我尝试显示插页式广告时,会导致应用程序崩溃。

07-14 15:30:22.534: E/AndroidRuntime(17681): FATAL EXCEPTION: main
07-14 15:30:22.534: E/AndroidRuntime(17681): Process: com.mydomain.MyApp, PID: 17681
07-14 15:30:22.534: E/AndroidRuntime(17681): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.mydomain.MyApp.fragments.TimerDisplayFragment$RoundSound)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeSerializable(Parcel.java:1316)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeValue(Parcel.java:1264)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeList(Parcel.java:653)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeValue(Parcel.java:1226)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Bundle.writeToParcel(Bundle.java:1692)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeBundle(Parcel.java:636)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.app.FragmentState.writeToParcel(Fragment.java:133)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeTypedArray(Parcel.java:1133)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:373)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeParcelable(Parcel.java:1285)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeValue(Parcel.java:1204)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Bundle.writeToParcel(Bundle.java:1692)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeBundle(Parcel.java:636)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2467)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3084)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Handler.handleCallback(Handler.java:733)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Looper.loop(Looper.java:136)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.app.ActivityThread.main(ActivityThread.java:5001)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.lang.reflect.Method.invokeNative(Native Method)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.lang.reflect.Method.invoke(Method.java:515)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at dalvik.system.NativeStart.main(Native Method)
07-14 15:30:22.534: E/AndroidRuntime(17681): Caused by: java.io.NotSerializableException: com.mydomain.MyApp.fragments.TimerDisplayFragment
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
07-14 15:30:22.534: E/AndroidRuntime(17681):    at android.os.Parcel.writeSerializable(Parcel.java:1311)
07-14 15:30:22.534: E/AndroidRuntime(17681):    ... 25 more

除了我试图序列化的类名之外,堆栈跟踪不会指向我的代码中的ant部分。

虽然我在搜索这个问题时遇到了麻烦,但我不能成为唯一有这个问题的人,因为我觉得这是一个相当常见的用例。

这是序列化的类。

    private class RoundSound implements Serializable{

    private static final long serialVersionUID = -6594386620517893228L;
    public String sound;
    public long delay;

    RoundSound(String s, long d){
        sound = s;
        delay = d;
    }       
}

以下是显示广告的代码。

public static void showInterstitial(Context ctx){
    if(interstitial == null){

        interstitial = new InterstitialAd(ctx);
        interstitial.setAdUnitId(MY_TEST_AD_UNUT);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("")
                .addTestDevice("")
                .build();
        interstitial.loadAd(adRequest);

    }
    if(actionsToAd <= 0 && !isPro){
        actionsToAd = 3;
        interstitial.show();
        interstitial = null;
    }else{
        actionsToAd--;
    }
}

如果有人能够为这个问题提供一些见解或解决方法,那将非常感激。

1 个答案:

答案 0 :(得分:0)

你的 RoundSound 类似乎是一个非静态的内部类。此类的每个实例都将引用外部类的实例( TimerDisplayFragment )。因此,当您序列化 RoundSound 实例时,它将编写实例的直接属性以及 TimerDisplayFragment 实例。但是,片段实例不可序列化,而且恰好是错误发生的位置。请注意,嵌套异常表示外部类不可序列化。它没有提到内部阶级。

要修复它,请尽可能将 RoundSound 类转换为静态内部类:

private static class RoundSound implements Serializable {
   ...
}