问题描述
我开发简单的旅行社android应用。当应用程序启动时加载的城市名单,未来活动的用户将看到源和目标纺纱,一旦用户选择了正确的源和目标,将采取第三个活动,它会显示可用的旅行,如果用户选择一个它需要第四个活动该用户选择的座位,并继续预定票。为了保持完整的用户会话我保持UserSession类,它是如下(在code这是无关的问题,消除不必要的逻辑)
I am developing simple travel agent android application. When application starts it loads list of cities, in the next activity user will see source and destination spinners, once user selects proper source and destination, it will take to third activity, it displays available travels if user selects one it takes to fourth activity in which user selects seat and continues to book ticket. In order to maintain complete user session I am maintaining a UserSession class, which is as follows (removing unnecessary logic in the code which is irrelevant to problems)
public class UserSession implements Parcelable {
List<City> citiesList;
HashMap<String, City> cityMap; // Map city name to code
RouteSearchResult searchedRoutes;
String sourceCity;
String destinationCity;
LocalStop selectedBoardingPoint;
LocalStop selectedArrivalPoint;
@Override
public void writeToParcel(Parcel parcel, int flags) {
try {
parcel.writeList(citiesList);
parcel.writeMap(cityMap);
parcel.writeValue(searchedRoutes);
parcel.writeString(sourceCity);
parcel.writeString(destinationCity);
parcel.writeValue(selectedBoardingPoint);
parcel.writeValue(selectedArrivalPoint);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class City implements Parcelable {
String cityName;
String cityId;
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(cityName);
parcel.writeString(cityId);
}
}
public class RouteSearchResult {
City source;
City destination;
String date;
List<RouteDetails> routeDetails;
}
我有以下问题
1)我得到的运行时异常而写
I have the following problems1) I am getting run time exception while writing
W/System.err( 817): java.lang.RuntimeException: Parcel: unable to marshal value com.travelagent.City@40605878
W/System.err( 817): at android.os.Parcel.writeValue(Parcel.java:1132)
W/System.err( 817): at android.os.Parcel.writeList(Parcel.java:519)
W/System.err( 817): at com.travelagent.UserSession.writeToParcel(UserSession.java:201)
W/System.err( 817): at android.os.Parcel.writeParcelable(Parcel.java:1151)
2)我评论 parcel.writeList(citiesList);
语句来看看,如果我得到任何更多的问题,我买了包裹类似的问题。 writeValue(searchedRoutes);
2) I commented parcel.writeList(citiesList);
statement to see if I get any more problems, I got similar problem with parcel.writeValue(searchedRoutes);
我有以下问题,
1)如何使作为parcelable自定义对象的名单?
2)是否必须使自定义类如城市
也parcelable,这是实际的类的一部分被瓜分。
3)是否有解决这个有点像传递用户会话的所有活动问题的任何做法。
I have following questions,1) How to make a list of custom objects as parcelable?2) Is it mandatory to make custom class like City
also parcelable, which is part of actual class to be parceled.3) Is there any approach to solve this kind of problems like passing user session to all activities.
请帮忙,我搜索,但我无法找到合适的解决方案,使自定义类parcelable。
Please help, I searched, but I couldn't find appropriate solution to make custom class parcelable.
推荐答案
我的理解是,你不应该使用这个包裹。从rel=\"nofollow\">包裹在
My understanding is that you shouldn't use Parcels for this. From the android documentation on Parcels:
包裹不是通用序列化机制。这个类(和用于将任意对象成包裹相应Parcelable API)被设计成一个高性能的IPC传输。因此,它不适合放置任何包裹数据中持久性存储:在任何的包裹数据的底层实现的改变可以使较旧的数据无法读取。
你应该做的是创造要转移到下一个活动每个班两个静态方法。叫他们中的一个 toBundle()
,其中之一 fromBundle()
。第一类,你的给定实例转换为包,而第二个从包创建类的新实例。每当你需要在传递一个对象到下一个活动,只是捆绑起来使用 toBundle()
方法,并把它添加到你使用启动的意图下一个活动。在接下来的活动中,你可以通过调用重新创建对象 fromBundle()
。
What you should do is create two static methods for each class you want to transfer to the next activity. Call one of them toBundle()
and one of them fromBundle()
. The first converts a given instance of you class to a bundle while the second creates a new instance of your class from a bundle. Every time you need to pass an object on to the next Activity, just bundle it up using the toBundle()
method and add it to the Intent you're using to start the next Activity. In the next Activity, you can recreate the object by calling fromBundle()
.
这篇关于Android的用户对象parcelable问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!