如何在Android Lollipop版本的Parcelable类中添加/保留Class <! - ? - >对象

时间:2015-04-14 09:34:50

标签: android class parcelable parcel

Parcelable向另一个Activity发送Class<?>数据时,我遇到了问题。

  

我的问题:是否可以在Parcelable类中添加/保留Activity对象,该类必须发送给另一个Android 5.0, 5.1 and 5.1.1?如果可能的话,我该如何实现呢?

我已经尝试过这样做,但我在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.access$800(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$1.createFromParcel(HomeItemCls.java:260) 04-14 14:58:36.838: E/AndroidRuntime(31164): at com.example.template.HomeItemCls$1.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$2.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 版本中遇到以下异常:

我的LOGCAT:

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];
        }
    };
}

我的代码:

{{1}}

1 个答案:

答案 0 :(得分:3)

由于Class实施Serializable而不Parcelable,您无法使用readValue / writeValue对(这些是Parcelable函数< /强>)。

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

或者,您也应该更改

Class<?> itemFragment;

Class<? extends Fragment> itemFragment;

private Class<?> itemActivity;

private Class<? extends Activity> itemActivity;