清除ram后,使用可打包传递数据得到java.lang.ClassCastException

时间:2019-02-21 07:43:32

标签: android ram classcastexception parcelable

我实现了Serializable和Parcelable两个类。

这样的数据类,Serializable和Parcelable的详细信息都被忽略。

public class Test1 implements Parcelable  {

public Test1() {
}

String s1;
String s2;
String s3;
String s4;
String s5;
String s6;

}

public class Test2 implements 
Serializable  {

public Test2() {
}

String s1;
String s2;
String s3;
String s4;
String s5;
String s6;

}

并编写两个类来扩展它们,像这样

public class test11<T> extends test1{

private List<T> list;

public test11(List<T> list) {
    this.list = list;
}


public List<T> getList() {
    return list;
}
 }

public class test22<T> extends test2{

private List<T> list;

public TextSSS2(List<T> list) {
    this.list = list;
}


public List<T> getList() {
    return list;
}
}

然后我将数据打包分发给片段

            Test1 test1 = new Test1();
            test1.setS1("1");

            List<Test1> list1 = new ArrayList<Test1>();
            list1.add(test1);
            Test11 test11 = new Test11(list1);


            Test2 test2 = new Test2();
            test2.setS1("2");


            List<Test2> list2 = new ArrayList<Test2>();
            list2.add(test2);
            Test22 test22 = new Test22(list2);

            Bundle bundle = new Bundle();
            bundle.putParcelable("test1", test11);
            bundle.putSerializable("test2", test22);

            Fragment fragment = new TestFragment();
            fragment.setArguments(bundle);

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.containerF, fragment, "AAA")
                    .addToBackStack(null)
                    .commit();

并获取片段中的数据

        Test11 test11 = getArguments().getParcelable("test1");
        Test22 test22 = (Test22) getArguments().getSerializable("test2");

        L.e("1", test11.toString());
        L.e("2", test22.toString());

在正常使用该应用程序时可以正常工作。但是,当我清除内存并返回到该应用程序后,ClassCastException将其崩溃了!

  Caused by: java.lang.ClassCastException: test2018.my.mynewtest.Test1 cannot be cast to test2018.my.mynewtest.Test11
    at test2018.my.mynewtest.TestFragment.updateData(TestFragment.java:53)
    at test2018.my.mynewtest.TestFragment.onCreateView(TestFragment.java:42)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269)
    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
    at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
    at android.app.Activity.performStart(Activity.java:6959)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2890)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6682) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1534) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1424) 

崩溃的行

 Test11 test11 = getArguments().getParcelable("test1");

我想知道它是怎么引起的。 谢谢!

1 个答案:

答案 0 :(得分:0)

看看如何implement可包裹。缺少一些东西,例如

  

称为CREATOR的非空静态字段

而且您不能在超级类型上实现Parcelable。必须为每个班级做。