gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:04:35 24 4
gpt4 key购买 nike

我在发送 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.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您必须将返回值转换为特定类型。

可选,你也应该改变

Class<?> itemFragment;

Class<? extends Fragment> itemFragment; ,

private Class<?> itemActivity;

private Class<? extends Activity> itemActivity;

关于android - 如何在 Android Lollipop 版本的 Parcelable 类中添加/保留 Class<?> 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29623905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com