从后台打开应用程序时,Fragment中的可序列化ViewPager导致Android应用程序崩溃

时间:2019-04-16 04:26:52

标签: android android-fragments android-viewpager serializable

我尝试在我的应用中将ViewPage传递给Fragement。但是,当应用程序被OS杀死后打开该应用程序时,该应用程序将崩溃;或者在开发人员选项中启用了“不保留活动”时,从后台重新打开该应用程序。

以下是Logcat的错误消息摘要:

(1)java.lang.RuntimeException:无法启动活动ComponentInfo {com.test3 / com.test.MainActivity}:java.lang.RuntimeException:Parcelable在读取可序列化对象(名称= com.test.VerticalViewPager)时遇到IOException

(2)原因:java.lang.RuntimeException:在读取可序列化对象(名称= com.test.VerticalViewPager)时遇到Parcelable IOException

(3)原因:java.io.InvalidClassException:com.test.VerticalViewPager;没有有效的构造函数

经过一些重新测试,我发现实现Serializable的Java类必须具有无参数构造函数(参考:http://burnignorance.com/java-web-development-tips/java-serialization-and-need-for-a-no-arg-constructor/)。 当我尝试向我的ViewPager类添加无参数构造函数时,Android Studio一直警告我“'android.support.v4.view.ViewPager'中没有默认的构造函数。”

我做了一整天的研究,但仍然不知道如何解决它。如果您有任何想法请告诉我,谢谢


这是Fragment类:

public final class FragmentTest extends Fragment {

    private static final String VERTICALVIEWPAGER = "VERTICALVIEWPAGER";

    public static FragmentTest newInstance(VerticalViewPager myVerticalViewPager) {
        FragmentTest f = new FragmentTest();
        Bundle args = new Bundle();
        args.putSerializable(VERTICALVIEWPAGER, myVerticalViewPager);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = (MainActivity) getActivity();

        // Error at this line when open the App after it was killed by OS (Or reopen the App from background while "Don't keep activities" is enabled )
        myVerticalViewPager = getArguments() != null ? (VerticalViewPager) (getArguments().getSerializable(VERTICALVIEWPAGER)) : null;
    }

}


这是ViewPager类:

public final class VerticalViewPager extends ViewPager implements Serializable{

    public VerticalViewPager(Context context) {
        super(context);
        init();
    }

    public VerticalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // The majority of the magic happens here
        setPageTransformer(true, new VerticalPageTransformer());
        // The easiest way to get rid of the overscroll drawing that happens on the left and right
        setOverScrollMode(OVER_SCROLL_NEVER);
    }

    .
    .
    .
}


以下是完整的Logcat消息:

  

2019-04-16 11:40:27.676 5369-5369 / com.test3 E / Android运行时:致命   例外:主要过程:com.test3,PID:5369   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.test3 / com.test.MainActivity}:   java.lang.RuntimeException:遇到Parcelable IOException读取   一个可序列化的对象(名称为com.test.VerticalViewPager),位于   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2812)     在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2890)     在android.app.ActivityThread.-wrap11(未知来源:0)处   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1611)     在android.os.Handler.dispatchMessage(Handler.java:106)   android.os.Looper.loop(Looper.java:171)在   android.app.ActivityThread.main(ActivityThread.java:6633)在   java.lang.reflect.Method.invoke(本机方法)位于   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)   引起原因:java.lang.RuntimeException:遇到了Parcelable   IOException读取可序列化的对象(名称=   com.test.VerticalViewPager)   android.os.Parcel.readSerializable(Parcel.java:3007)在   android.os.Parcel.readValue(Parcel.java:2797)在   android.os.Parcel.readArrayMapInternal(Parcel.java:3115)位于   android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)     在android.os.BaseBundle.unparcel(BaseBundle.java:226)处   android.os.BaseBundle.getSerializable(BaseBundle.java:1199)在   android.os.Bundle.getSerializable(Bundle.java:1033)在   com.test.view.FragmentTest.onCreate(FragmentTest.java:79)在   android.support.v4.app.Fragment.performCreate(Fragment.java:2339)在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1377)     在   android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)     在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)     在   android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217)     在   android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:3166)     在   android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:181)     在   android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)     在   android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)     在com.test.MainActivity.onCreate(MainActivity.java:364)处   android.app.Activity.performCreate(Activity.java:7084)在   android.app.Activity.performCreate(Activity.java:7075)在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)     在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2765)     在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2890)     在android.app.ActivityThread.-wrap11(未知来源:0)   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1611)     在android.os.Handler.dispatchMessage(Handler.java:106)   android.os.Looper.loop(Looper.java:171)在   android.app.ActivityThread.main(ActivityThread.java:6633)在   java.lang.reflect.Method.invoke(本机方法)在   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)   造成原因:java.io.InvalidClassException:com.test.VerticalViewPager;   处没有有效的构造函数   java.io.ObjectStreamClass $ ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:147)     在   java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:757)     在   java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1778)     在java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)     在java.io.ObjectInputStream.readObject(ObjectInputStream.java:374)     在android.os.Parcel.readSerializable(Parcel.java:3005)处   android.os.Parcel.readValue(Parcel.java:2797)在   android.os.Parcel.readArrayMapInternal(Parcel.java:3115)在   android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:273)     在android.os.BaseBundle.unparcel(BaseBundle.java:226)处   android.os.BaseBundle.getSerializable(BaseBundle.java:1199)在   android.os.Bundle.getSerializable(Bundle.java:1033)在   com.test.view.FragmentTest.onCreate(FragmentTest.java:79)在   android.support.v4.app.Fragment.performCreate(Fragment.java:2339)在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1377)     在   android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)     在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)     在   android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217)     在   android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:3166)     在   android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:181)     在   android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:319)     在   android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)     在com.test.MainActivity.onCreate(MainActivity.java:364)处   android.app.Activity.performCreate(Activity.java:7084)在   android.app.Activity.performCreate(Activity.java:7075)在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)     在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2765)     在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2890)     在android.app.ActivityThread.-wrap11(未知来源:0)   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1611)     在android.os.Handler.dispatchMessage(Handler.java:106)   android.os.Looper.loop(Looper.java:171)在   android.app.ActivityThread.main(ActivityThread.java:6633)在   java.lang.reflect.Method.invoke(本机方法)在   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:547)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

0 个答案:

没有答案