将Parcelable数据从一个活动发送到另一个活动时遇到问题.
MY QUESTION: Is it possible to add/keep
Class<?>
object inside aParcelable
class which has to be sent to anotherActivity
? If possible then how can I achieve it?
我已经尝试过这样做,但是在Android 5.0、5.1和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.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
我的密码:
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,您必须将返回值转换为特定类型.
(可选)您还应该更改
类<?> itemFragment;
至
类< ;?延伸Fragment> itemFragment ;,
和
私人Class<?> itemActivity;
至
私人班级< ;?扩展Activity> itemActivity;