如何在Android中使用Parcel?
我正在尝试使用 Parcel
写入并读回 Parcelable
。由于某种原因,当我从文件中读回对象时,它返回为 null
。
public void testFoo() {
final Foo orig = new Foo("blah blah");
// Wrote orig to a parcel and then byte array
final Parcel p1 = Parcel.obtain();
p1.writeValue(orig);
final byte[] bytes = p1.marshall();
// Check to make sure that the byte array seems to contain a Parcelable
assertEquals(4, bytes[0]); // Parcel.VAL_PARCELABLE
// Unmarshall a Foo from that byte array
final Parcel p2 = Parcel.obtain();
p2.unmarshall(bytes, 0, bytes.length);
final Foo result = (Foo) p2.readValue(Foo.class.getClassLoader());
assertNotNull(result); // FAIL
assertEquals( orig.str, result.str );
}
protected static class Foo implements Parcelable {
protected static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() {
public Foo createFromParcel(Parcel source) {
final Foo f = new Foo();
f.str = (String) source.readValue(Foo.class.getClassLoader());
return f;
}
public Foo[] newArray(int size) {
throw new UnsupportedOperationException();
}
};
public String str;
public Foo() {
}
public Foo( String s ) {
str = s;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int ignored) {
dest.writeValue(str);
}
}
我缺少什么?
更新:为了简化测试,我在原始示例中删除了文件的读取和写入。
I'm trying to use Parcel
to write and then read back a Parcelable
. For some reason, when I read the object back from the file, it's coming back as null
.
public void testFoo() {
final Foo orig = new Foo("blah blah");
// Wrote orig to a parcel and then byte array
final Parcel p1 = Parcel.obtain();
p1.writeValue(orig);
final byte[] bytes = p1.marshall();
// Check to make sure that the byte array seems to contain a Parcelable
assertEquals(4, bytes[0]); // Parcel.VAL_PARCELABLE
// Unmarshall a Foo from that byte array
final Parcel p2 = Parcel.obtain();
p2.unmarshall(bytes, 0, bytes.length);
final Foo result = (Foo) p2.readValue(Foo.class.getClassLoader());
assertNotNull(result); // FAIL
assertEquals( orig.str, result.str );
}
protected static class Foo implements Parcelable {
protected static final Parcelable.Creator<Foo> CREATOR = new Parcelable.Creator<Foo>() {
public Foo createFromParcel(Parcel source) {
final Foo f = new Foo();
f.str = (String) source.readValue(Foo.class.getClassLoader());
return f;
}
public Foo[] newArray(int size) {
throw new UnsupportedOperationException();
}
};
public String str;
public Foo() {
}
public Foo( String s ) {
str = s;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int ignored) {
dest.writeValue(str);
}
}
What am I missing?
UPDATE: To simplify the test I've removed the reading and writing of files in my original example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
啊,终于找到问题了。事实上有两个。
setDataPosition(0)
。这是修改后的工作代码:
Ah, I finally found the problem. There were two in fact.
setDataPosition(0)
after unmarshalling your data.Here is the revised, working code:
提防!不要使用 Parcel 序列化到文件
来自 http://developer.android.com/reference/android/os/Parcel。 html
Beware! Dont use Parcel for serialization to a file
from http://developer.android.com/reference/android/os/Parcel.html
我发现 Parcelable 最常在 Android 中的数据包中使用,但更具体地说,是在发送和接收消息的处理程序中使用。例如,您可能有一个
AsyncTask
或Runnable
需要在后台运行,但将结果数据发布到主线程或Activity
。这是一个简单的例子。如果我有一个如下所示的 Runnable:
如您所见,此 Runnable 在其构造函数中采用 Handler。这是从一些
Activity
中调用的,如下所示:现在,剩下的就是这个问题的核心,即如何将类定义为
Parcelable
。我选择了一个相当复杂的类来展示,因为有些事情在简单的类中是看不到的。下面是ProductInfo
类,它可以干净地进行 Parcel 和 unParcels:CREATOR
至关重要,生成的构造函数采用 Parcel 也是如此。我包含了更复杂的数据类型,以便您可以了解如何对可 Parcelable 对象的数组进行 Parcel 和 unParcel 数组。当使用 Gson 将 JSON 转换为具有子对象的对象时,这是很常见的事情,如本例所示。I find that Parcelable is most often used in Android within data Bundles, but more specifically within a Handler that is sending and receiving messages. As an example, you might have an
AsyncTask
or aRunnable
that needs to run in the background but post resulting data to the Main thread orActivity
.Here's a simple example. If I have a
Runnable
that looks like this:As you can see, this runnable takes a Handler in its constructor. This is called from some
Activity
like this:Now, all that is left is the heart of this question, how you define a class as
Parcelable
. I've chosen a fairly complex class to show because there are some things you would not see with a simple one. Here is theProductInfo
class, which Parcels and unParcels cleanly:The
CREATOR
is critical, as is the resulting constructor taking a Parcel. I included the more complex data types so you could see how to Parcel and unParcel Arrays of Parcelable objects. This is a common thing when using Gson to convert JSON into objects with children as in this example.要更好地理解包裹概念,请尝试以下链接
希望这有帮助:)
To get a better understanding of the Parcel concept Try the below Link
hope this helps :)
我也有类似的问题。只有以下来自 emmby 和 this 的片段帮助了我。
它应该保存在每个实现 Parcelable 的类中
I too had similar problem. only the following snippet from emmby and this helped me out.
It should be kept in each of the class that implements Parcelable