我如何知道 Parcel.readStringArray() 的数组有多大?
执行此操作时出现空指针异常:
private String[] foo;
private void readFromParcel(Parcel in) {
in.readStringArray(foo);
}
看来我需要先分配数组。但我怎么知道要制作多大呢?是否必须先写尺寸,然后再读取尺寸?难道不是有一个 writeStringArray() 方法来为我处理这个问题吗?
来自 Android 文档:
有多种方法可以读取和写入原始对象的原始数组,这些方法通常会写入 4 字节长度,后跟原始数据项。
所以,如果我自己写计数,它就会被写两次。必须有某种方法可以做到这一点,而我不负责管理它。
I'm getting a null pointer exception when I do this:
private String[] foo;
private void readFromParcel(Parcel in) {
in.readStringArray(foo);
}
It seems I need to allocate the array first. But how do I know how big to make it? Do I have to write the size first and then read the size first? And isn't the point of having a writeStringArray()
method to handle this for me?
From the Android Documentation:
There are a variety of methods for reading and writing raw arrays of primitive objects, which generally result in writing a 4-byte length followed by the primitive data items.
So, if I write the count myself, it's getting written twice. There must be some way of doing this where I am not responsible for managing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这不是直接答案,但您可以使用列表 (
ArrayList
) 代替writeList()
和createStringArrayList()
。或者您可以使用
Parcle#createStringArray()< /code>
获取数组。
I know this is not a direct answer, but you can use lists (
ArrayList<String>
) instead withwriteList()
andcreateStringArrayList()
.Or you could use
Parcle#createStringArray()
to get the array.我希望你必须做这样的事情:
这意味着你必须在写出时记录数组的大小。
I'd expect you'd have to do something like this:
which implies you have to record the size of the array when writing out.