本文介绍了在android系统实现Parcelable时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的类实现Parcelable类
包mobile.bh.classes;进口的java.util.ArrayList;
进口的java.util.List;进口mobile.bh.activities.MethodStep;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.os.Parcel;
进口android.os.Parcelable;//简单的类,只是有一个成员属性为例
公共类配方实现Parcelable {
公众诠释身份证;
公共字符串名称;
公开名单<性成分GT;成分;
公开名单< MethodStep>方法;
公共字符串评论;
公共字符串形象;
公共位图图像2;
上下文mContext; 公开配方(上下文C){
mContext = C;
}
/ *下面的一切这里是实现Parcelable * /
公众诠释describeContents(){
返回0;
} //写对象的数据传入的包裹
公共无效writeToParcel(包裹出来,诠释标志){
out.writeInt(ID);
out.writeString(名);
out.writeList(成分);
out.writeList(方法);
out.writeString(注解);
out.writeString(图片);
} //这是用来重新生成对象。所有Parcelables并必须有一个实现这两种方法CREATOR
公共静态最终Parcelable.Creator<&配方GT; CREATOR =新Parcelable.Creator<&配方GT;(){
公共配方createFromParcel(包裹中){
返回新配方(以);
} 公共食谱[] newArray(INT大小){
返回新配方【尺寸】;
}
};
//例如构造函数地块,并为您填充它的值的对象
私人配方(包裹中){
ID = in.readInt();
名称= in.readString();
配料=(ArrayList的<性成分GT;)in.readSerializable();
方法=(ArrayList的< MethodStep>)in.readSerializable();
注释= in.readString();
图像= in.readString();
}
}
但我得到以下错误,当它读名称
:
09-03 16:15:35.364:E / AndroidRuntime(330):了java.lang.RuntimeException:致Parcelable遇到IOException异常读取序列化对象(名称=)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Parcel.readSerializable(Parcel.java:2022)
。09-03 16:15:35.364:E / AndroidRuntime(330):在mobile.bh.classes.Recipe<&初始化GT;(Recipe.java:61)
。09-03 16:15:35.364:E / AndroidRuntime(330):在mobile.bh.classes.Recipe<&初始化GT;(Recipe.java:56)
09-03 16:15:35.364:E / AndroidRuntime(330):在mobile.bh.classes.Recipe $ 1.createFromParcel(Recipe.java:48)
09-03 16:15:35.364:E / AndroidRuntime(330):在mobile.bh.classes.Recipe $ 1.createFromParcel(Recipe.java:1)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Parcel.readParcelable(Parcel.java:1981)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Parcel.readValue(Parcel.java:1846)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Parcel.readListInternal(Parcel.java:2092)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Parcel.readArrayList(Parcel.java:1536)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Parcel.readValue(Parcel.java:1867)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Parcel.readMapInternal(Parcel.java:2083)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Bundle.unparcel(Bundle.java:208)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.content.Intent.getParcelableArrayListExtra(Intent.java:3448)
09-03 16:15:35.364:E / AndroidRuntime(330):在mobile.bh.activities.RecipeInfoActivity.onCreate(RecipeInfoActivity.java:56)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-03 16:15:35.364:E / AndroidRuntime(330):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
解决方案
当您使用
out.writeList(成分);
out.writeList(方法);
要写入数据到地块,你应该使用
=成分in.readList();
方法= in.readList();
阅读。而不是 readSerializable()
好运气
The following class implements Parcelable class
package mobile.bh.classes;
import java.util.ArrayList;
import java.util.List;
import mobile.bh.activities.MethodStep;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
//simple class that just has one member property as an example
public class Recipe implements Parcelable {
public int id;
public String name;
public List<Ingredient> ingredients;
public List<MethodStep> method;
public String comment;
public String image;
public Bitmap image2;
Context mContext;
public Recipe(Context c){
mContext = c;
}
/* everything below here is for implementing Parcelable */
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel out, int flags) {
out.writeInt(id);
out.writeString(name);
out.writeList(ingredients);
out.writeList(method);
out.writeString(comment);
out.writeString(image);
}
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<Recipe> CREATOR = new Parcelable.Creator<Recipe>() {
public Recipe createFromParcel(Parcel in) {
return new Recipe(in);
}
public Recipe[] newArray(int size) {
return new Recipe[size];
}
};
// example constructor that takes a Parcel and gives you an object populated with it's values
private Recipe(Parcel in) {
id = in.readInt();
name =in.readString();
ingredients = (ArrayList<Ingredient>)in.readSerializable();
method = (ArrayList<MethodStep>)in.readSerializable();
comment = in.readString();
image = in.readString();
}
}
But I got the following error when it reads name
:
09-03 16:15:35.364: E/AndroidRuntime(330): Caused by: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = )
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Parcel.readSerializable(Parcel.java:2022)
09-03 16:15:35.364: E/AndroidRuntime(330): at mobile.bh.classes.Recipe.<init>(Recipe.java:61)
09-03 16:15:35.364: E/AndroidRuntime(330): at mobile.bh.classes.Recipe.<init>(Recipe.java:56)
09-03 16:15:35.364: E/AndroidRuntime(330): at mobile.bh.classes.Recipe$1.createFromParcel(Recipe.java:48)
09-03 16:15:35.364: E/AndroidRuntime(330): at mobile.bh.classes.Recipe$1.createFromParcel(Recipe.java:1)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Parcel.readParcelable(Parcel.java:1981)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Parcel.readValue(Parcel.java:1846)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Parcel.readListInternal(Parcel.java:2092)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Parcel.readArrayList(Parcel.java:1536)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Parcel.readValue(Parcel.java:1867)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Parcel.readMapInternal(Parcel.java:2083)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Bundle.unparcel(Bundle.java:208)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.content.Intent.getParcelableArrayListExtra(Intent.java:3448)
09-03 16:15:35.364: E/AndroidRuntime(330): at mobile.bh.activities.RecipeInfoActivity.onCreate(RecipeInfoActivity.java:56)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-03 16:15:35.364: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
解决方案
When you use
out.writeList(ingredients);
out.writeList(method);
To write data to Parcel, you should use
ingredients = in.readList();
method = in.readList();
to read it. And not readSerializable()
Good luck
这篇关于在android系统实现Parcelable时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!