简体   繁体   English

在Android(Java)上序列化复杂对象

[英]Serializing Complex Objects on Android (Java)

What I'm trying to do is send an object through Intents; 我想做的是通过Intents发送对象; I've been looking at implementing Serializable like so: 我一直在像这样实现Serializable:

Intent viewRecipe = new Intent(t, RecipeView.class);
Bundle input = new Bundle();
input.putSerializable("myRecipe", recipes.ReturnRecipe(position).toString());
viewRecipe.putExtras(input);
startActivity(viewRecipe);

However, I'm guessing because I have a complex class it isn't working. 但是,我正在猜测,因为我有一个复杂的类,它无法正常工作。 Can anyone help explain how i would be able to convert this object to a Serialized string? 谁能帮忙解释一下我将如何将此对象转换为序列化字符串? Also it would be helpful if you know of an easier/simpler way of doing it; 如果您知道一种更简单的方法,也将有所帮助; may also be worth noting I plan to integrate it with a SQL database further down the line, but I'm stuck at the moment. 也许还值得一提的是,我计划将其与SQL数据库进一步集成,但此刻我陷入了困境。

The class is: 该类是:

public class Recipe implements Serializable {

int mRating;
String mName;
String mDescription;

ArrayList<Ingredient> ingredients;
ArrayList<Step> instructions;
...
}

I'm not sure if i have to override "readObject" and "writeObject", or even how I would start to do that. 我不确定是否必须覆盖“ readObject”和“ writeObject”,或者甚至我将如何开始这样做。 As a side note, Ingredient and Step are not complex classes: 附带说明一下,Ingredient和Step不是复杂的类:

public class Ingredient {

String mName;
int mValue;
String mMeasurement;
    ...
}

public class Step {

int mIndex;
String task;
Date time;
    ...
}

To cut a long story short, I have no idea what I'm doing and could use some guidence. 简而言之,我不知道自己在做什么,可以使用一些指导。 I've read a fair bit on how to serialize simple classes but they haven't really helped. 我已经对如何序列化简单的类有一定的了解,但是它们并没有真正的帮助。

EDIT: Including the error im getting, just imeplementing Serilizable gets me this error when i retrieve the object from the second acitivity. 编辑:包括错误即时获取,只是当我从第二个主动性中检索对象时,只是实现Serilizable就让我收到此错误。

Intent myIntent = getIntent();
    Bundle extras = myIntent.getExtras(); //breaks on this line

    currentRecipe = (Recipe) extras.get("myRecipe");

02-29 15:15:56.521: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pocket.recipes/com.pocket.recipes.RecipeView}: java.lang.ClassCastException: java.lang.String cannot be cast to com.pocket.recipes.Recipe

Above is the line shown after "FATAL EXCEPTION: main" 上方是“致命异常:主”之后显示的行

I then have a lot of errors merely saying "at android.app...." 然后我有很多错误,只说“在android.app ....”

I also have this line in there: 我那里也有这行:

02-29 15:15:56.521: E/AndroidRuntime(534): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.pocket.recipes.Recipe

Sorry about this, I'm new to Java / Android 抱歉,我是Java / Android新手

Unless you can't avoid it, you shouldn't use Serializable when sending data via Intents (due to performance concerns). 除非无法避免,否则在通过Intents发送数据时(由于性能问题),不应使用Serializable。 You should implement the Parcelable interface instead. 您应该改为实现Parcelable接口。 There's a lot of cruft, but it's actually pretty simple, unless you have an extremely complex class. 有很多问题,但实际上非常简单,除非您的类非常复杂。 You can also implement Parcelable on the child classes, and then just use writeParcelableArray() for your lists of ingredients and steps. 您还可以在子类上实现writeParcelableArray() ,然后仅对成分和步骤列表使用writeParcelableArray()

I had the similar problem. 我有类似的问题。 After a long and tedious debugging, I realized that all the classes whose instances are used as member variables, are to be serialized. 经过冗长乏味的调试,我意识到所有实例都将用作成员变量的类都将被序列化。 So, in your case, class Ingredient and class Step also need to be serialized, along with the class Recipe . 因此,在您的情况下,类IngredientStep以及类Recipe也需要序列化。 I hope this helps :) 我希望这有帮助 :)

You don't need to override methods. 您不需要重写方法。 Implementing interface should be enough. 实现接口应该足够了。 You haven't specified exactly what is not working, but complexity shouldn't be an issue as long as object you are trying to serialize has data which is serializable. 您没有确切指定什么不起作用,但是只要您要序列化的对象具有可序列化的数据,复杂性就不会成为问题。

You don't need to implement anything. 您无需执行任何操作。 Just declaring the "implement Serializable" make the instances of the class serializable. 只需声明“实现Serializable”即可使该类的实例可序列化。 as written in the documentation (http://developer.android.com/reference/java/io/Serializable.html) "Implementing this interface is enough to make most classes serializable." 如文档(http://developer.android.com/reference/java/io/Serializable.html)中所述,“实现此接口足以使大多数类可序列化。”

It depends on the context of the application, I think. 我认为这取决于应用程序的上下文。 Parcelable is one option, but other apps implement a ContentProvider and pass a URL or an id in the Intent, which are then used to lookup the item in the ContentProvider. 可打包是一种选择,但是其他应用程序实现ContentProvider并在Intent中传递URL或ID,然后将其用于在ContentProvider中查找项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM