包裹,类加载器和阅读Null值 [英] Parcel, ClassLoader and Reading Null values

查看:250
本文介绍了包裹,类加载器和阅读Null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解如何从包裹正确读取空值。让我们看一下我的使用情况:

 公共类为MyModel实现Parcelable {
    私人长期身份证;
    私人字符串名称;    公共为MyModel(){}    公众为MyModel(包裹中){
        readFromParcel(在);
    }    @覆盖公众诠释describeContents(){
        返回0;
    }    @覆盖公共无效writeToParcel(DEST包裹,诠释标志){
        dest.writeValue(ID);
        dest.writeValue(名);
    }    公共无效readFromParcel(包裹中){
        ID =(长)in.readValue(Long.class.getClassLoader());
        名称=(字符串)in.readValue(String.class.getClassLoader());
    }
}

有关这个问题的缘故,让我们现场的一个例子名称字符串类型。

我明白我可以使用 writeString 而不是 writeValue ,但 writeString 将抛出 NullPointerException异常如果名称为null。

所以,现在真正的问题是,如果名称为空,我试图 readValue ,然后铸造为null 字符串将抛出一个异常。

什么是在这种情况下,从包裹读取空值的最佳方法?
我需要每次我尝试从包裹到读取时间检查null?但是,如果你有很多的模型领域,这将是太多了。
A 的ClassLoader 阅读时路过之间的差异还是?
如果不读包裹时,值为null类加载器返回一个空/默认对象?

我通常得到的错误是这样的,当读取字符串从包裹中的一个恰巧:

了java.lang.RuntimeException:无法启动活动ComponentInfo {} com.company.MyActivity:了java.lang.RuntimeException:地块android.os.Parcel@42b82908:解组未知类型code 7209045偏移量1368


解决方案

  

什么是在从包裹读取空值的最佳方法
  这样吗?


可以使用特定字符串的方法:


  • Parcel.writeString(字符串)

  • Parcel.readString()

他们的工作非常顺利地使用值。


例如,假设你有以下字段一类是:

 公共类测试实现Parcelable {
    公众最终诠释ID;
    私人最终字符串名称;
    公众最终字符串描述;
    ...

您创建(使用Android工作室autoparcelable工具)这样的 Parcelable 实施

  @覆盖
公共无效writeToParcel(DEST包裹,INT标志){
    dest.writeInt(ID);
    dest.writeString(NULL); //这里要注意的空值
    dest.writeString(介绍);
}保护测试(包裹中){
    ID = in.readInt();
    名称= in.readString();
    说明= in.readString();
}

Test.name 值将正确地deserialised为,而不需要任何铸造字符串

I am trying to understand how to read null values from Parcel properly. Let's look my use case:

public class MyModel implements Parcelable {
    private Long id;
    private String name;

    public MyModel() {}

    public MyModel(Parcel in) {
        readFromParcel(in);
    }

    @Override public int describeContents() {
        return 0;
    }

    @Override public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(id);
        dest.writeValue(name);
    }

    public void readFromParcel(Parcel in) {
        id = (Long) in.readValue(Long.class.getClassLoader());
        name = (String) in.readValue(String.class.getClassLoader());
    }
}

For the sake of this question, let's take an example of field name which is of String type.

I understand I can use writeString instead of writeValue, but writeString will throw NullPointerException if name was null.

So now the real issue is, if name was null and i attempt to readValue and then casting null to String will throw an Exception.

What is the best approach to read null values from Parcel in that case? Do I need to check for null every time I attempt to read from Parcel? But that will be too much if you have a lot of fields in a model. Is there a difference between passing null or a ClassLoader when reading? Does a class loader returns an empty/default object if the value is null when reading Parcel?

The error I usually get is this, which happens when it is reading one of the Strings from Parcel:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.MyActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@42b82908: Unmarshalling unknown type code 7209045 at offset 1368

解决方案

What is the best approach to read null values from Parcel in that case?

You can use the string-specific methods:

  • Parcel.writeString(String)
  • Parcel.readString()

They work perfectly well with null values.


For example, assume you have a class with the following fields in it:

public class Test implements Parcelable {
    public final int id;
    private final String name;
    public final String description;
    ...

You create the Parcelable implementation like this (using Android Studio autoparcelable tool):

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(null); // NOTE the null value here
    dest.writeString(description);
}

protected Test(Parcel in) {
    id = in.readInt();
    name = in.readString();
    description = in.readString();
}

Your Test.name value will be deserialised as null correctly, without requiring any casting to String.

这篇关于包裹,类加载器和阅读Null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆