如何 add/keep Class<?> Android Lollipop 版本中的 Parcelable class 中的对象

How to add/keep Class<?> object inside a Parcelable class in Android Lollipop versions

我在将 Parcelable 数据从一个 Activity 发送到另一个时遇到问题。

MY QUESTION: Is it possible to add/keep Class<?> object inside a Parcelable class which has to be sent to another Activity? If possible then how can I achieve it?

我已经试过了,但在 Android 5.0, 5.1 and 5.1.1 版本中出现以下异常:

我的LOGCAT:

04-14 14:58:36.838: E/AndroidRuntime(31164): FATAL EXCEPTION: main
04-14 14:58:36.838: E/AndroidRuntime(31164): Process: com.example, PID: 31164
04-14 14:58:36.838: E/AndroidRuntime(31164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.v2.BaseAct}: java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = java.lang.Class)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.app.ActivityThread.access0(ActivityThread.java:151)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Looper.loop(Looper.java:135)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.app.ActivityThread.main(ActivityThread.java:5254)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.lang.reflect.Method.invoke(Native Method)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.lang.reflect.Method.invoke(Method.java:372)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-14 14:58:36.838: E/AndroidRuntime(31164): Caused by: java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = java.lang.Class)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readSerializable(Parcel.java:2384)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readValue(Parcel.java:2203)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at com.example.template.HomeItemCls.readFromParcel(HomeItemCls.java:251)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at com.example.template.HomeItemCls.<init>(HomeItemCls.java:213)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at com.example.template.HomeItemCls.createFromParcel(HomeItemCls.java:260)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at com.example.template.HomeItemCls.createFromParcel(HomeItemCls.java:1)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readParcelable(Parcel.java:2252)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readValue(Parcel.java:2152)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readListInternal(Parcel.java:2526)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readArrayList(Parcel.java:1842)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readValue(Parcel.java:2173)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.BaseBundle.unparcel(BaseBundle.java:221)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.BaseBundle.containsKey(BaseBundle.java:269)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.content.Intent.hasExtra(Intent.java:4879)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at com.example.v2.BaseAct.onCreate(BaseAct.java:184)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.app.Activity.performCreate(Activity.java:5990)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
04-14 14:58:36.838: E/AndroidRuntime(31164):    ... 10 more
04-14 14:58:36.838: E/AndroidRuntime(31164): Caused by: java.lang.ClassNotFoundException: com.example.v2.fragment.SongListFragment
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.lang.Class.classForName(Native Method)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.lang.Class.forName(Class.java:309)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.resolveClass(Parcel.java:2370)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1641)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:657)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.io.ObjectInputStream.readNewClass(ObjectInputStream.java:1512)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:755)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1983)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1940)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at android.os.Parcel.readSerializable(Parcel.java:2378)
04-14 14:58:36.838: E/AndroidRuntime(31164):    ... 28 more
04-14 14:58:36.838: E/AndroidRuntime(31164): Caused by: java.lang.ClassNotFoundException: com.example.v2.fragment.SongListFragment
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.lang.Class.classForName(Native Method)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
04-14 14:58:36.838: E/AndroidRuntime(31164):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
04-14 14:58:36.838: E/AndroidRuntime(31164):    ... 38 more
04-14 14:58:36.838: E/AndroidRuntime(31164): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我的代码:

public class HomeItemCls implements Parcelable
{
    private Class<?> itemActivity;

    private Class<?> itemFragment;

    public Class<?> getItemActivity()
    {
        return itemActivity;
    }
    public void setItemActivity(Class<?> itemActivity)
    {
        this.itemActivity = itemActivity;
    }

    public void setItemFragment(Class<?> itemFragment)
    {
        this.itemFragment = itemFragment;
    }

    public Class<?> getItemFragment()
    {
        return itemFragment;
    }
    public HomeItemCls()
    {
        // TODO Auto-generated constructor stub
    }

    public HomeItemCls(Parcel in)
    {
        readFromParcel(in);
    }

    @Override
    public int describeContents()
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags)
    {
        // TODO Auto-generated method stub
        dest.writeValue(getItemFragment());
        dest.writeValue(getItemActivity());
    }

    @SuppressWarnings("unchecked")
    private void readFromParcel(Parcel in)
    {
        // TODO Auto-generated method stub
        setItemFragment((Class<?>) in.readValue(Class.class.getClassLoader()));
        setItemActivity((Class<?>) in.readValue(Class.class.getClassLoader()));
    }

    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
    {
        public HomeItemCls createFromParcel(Parcel in)
        {
            return new HomeItemCls(in);
        }

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

由于 Class 实现 Serializable 而不是 Parcelable,您不能使用 readValue / writeValue 对(这些是 Parcelable 函数 )。

因此,您应该使用 writeSerializable/readSerializable 对(这些是 Serializable 函数 )。使用 readSerializable 您必须将返回值转换为特定类型。

可选,您还应该更改

Class<?> itemFragment;

Class<? extends Fragment> itemFragment;,

private Class<?> itemActivity;

private Class<? extends Activity> itemActivity;