如何避免Realm.io“试图在空对象引用上调用虚拟方法'boolean io.realm.ProxyState.isUnderConstruction()'”错误

时间:2019-03-06 18:24:34

标签: java android realm realm-java

更新:

目前,我无法创建任何新的基于Realm的对象。不管我是否清理,重建等,每一个都失败,并返回相同的错误。为了测试这一点,我创建了一个新类“ Sample”:

package com.reddragon.intouch.model;

import java.util.UUID;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

public class Sample extends RealmObject {
    @PrimaryKey
    private String id;
    private String sampleField;

    public Sample() {
        this(UUID.randomUUID().toString());
    }
    private Sample(String id) {
        this.id=id;
    }
    public String getId(){
        return id;
    }

    public String getSampleField() {
        return sampleField;
    }

    public void setSampleField(String sampleField) {
        this.sampleField = sampleField;
    }
}

然后在MainActivity.java中,尝试创建一个新实例:

    try {
        MediaDataMgr.get().addSample(new Sample());
        Timber.d("Lifecycle: was able to add Sample");
    } catch (Exception e) {
        Timber.d("Got exception instantiating Sample: %s", e.getMessage());
    }

addSample()方法对本项目中两个可以使用 DO 的类使用类似的方法:

public String addSample(Sample s) {
    boolean success = true;
    Sample sample;
    Realm realm;
    String retVal = null;
    boolean mainThread = Thread.currentThread().equals(Looper.getMainLooper().getThread());
    if (mainThread) {
        realm = mUIThreadRealm;
    } else {
        realm = Realm.getDefaultInstance();
    }
    try {
        realm.beginTransaction();
        sample = realm.createObject(Sample.class,s.getId()); //<--CRASH!!!!
        sample.setSampleField(s.getSampleField());
    } catch (Exception e ) {
        Timber.d( "Exception adding a Sample: %s", e.getMessage());
        success = false;
    } finally  {

        if ( success ) {
            realm.commitTransaction();
            retVal = s.getId();
        } else {
            realm.cancelTransaction();
        }
        if (!mainThread) {
            realm.close();
        }
    }
    return retVal;
}

我现在完全沉迷于这个项目。

更新:

我在我的应用程序中完全注释了对“联系人”对象的所有引用,然后从我的项目中删除了Contact.java。我进行了完整的重建,然后运行它,一切正常。

然后,我创建了一个新类Contact.java,并输入了与以前相同的字段等,并在项目的其余部分中对它进行了未注释的引用。我进行了重建并运行-并得到了相同的错误。

然后我将Contact类的名称重构为 ContactSharingInfo ,以为某个类名称可能发生冲突。重建并运行,然后再次-同样的错误,这次引用了新的类名。

原始帖子:

我正在使用gradle插件和注释处理器5.9.1。我创建了一个新类(“ Contact.java”),最初该类工作正常。然后,我调整了类(删除了几个字段,添加了一个新字段),然后开始收到此错误。我已经在Samsung S7 Edge(API 26)以及一些仿真器上对此进行了测试。同样的问题。

我已经进行了各种清理,重建,使缓存无效和重新启动等方式。没有帮助。我检查了错误#3819#4579,但是那里没有任何帮助。我已禁用即时运行。没有帮助。

堆栈跟踪为:

realmSet$id:111, com_reddragon_intouch_model_ContactRealmProxy (io.realm)
<init>:30, Contact (com.reddragon.intouch.model)
<init>:26, Contact (com.reddragon.intouch.model)
<init>:84, com_reddragon_intouch_model_ContactRealmProxy (io.realm)
newInstance:96, DefaultRealmModuleMediator (io.realm)
createObjectInternal:1048, Realm (io.realm)
createObject:1024, Realm (io.realm)
addContact:877, MediaDataMgr (com.reddragon.intouch.model)

导致此问题的代码(我在RealData集中访问的MediaDataMgr类中的addContact())是:

    public String addContact(Contact c, int status) {
        boolean success = true;
        Contact contact;
        Realm realm;
        String retVal = null;
        boolean mainThread = Thread.currentThread().equals(Looper.getMainLooper().getThread());
        if (mainThread) {
            realm = mUIThreadRealm;
        } else {
            realm = Realm.getDefaultInstance();
        }
        try {
            realm.beginTransaction();
            contact = realm.createObject(Contact.class,c.getId()); // <--CRASH HAPPENS HERE
            contact.setEmailAddress(c.getEmailAddress());
            contact.setDisplayName(c.getDisplayName());
            contact.setStatus(status);

        } catch (Exception e ) {
            Timber.d( "Exception adding a contact: %s", e.getMessage());
            success = false;
        } finally  {

                if ( success ) {
                    realm.commitTransaction();
                    retVal = c.getId();
            } else {
                realm.cancelTransaction();
            }
            if (!mainThread) {
                realm.close();
            }
        }
        return retVal;
    }

并且堆栈跟踪中引用的Contact类是:

public class Contact extends RealmObject implements CardListable {
    @PrimaryKey
    private String id;
    private String displayName;
    private String emailAddress;
    private String pathToProfilePic; // This will always be a URI, but we have to store it as a string and convert to URI at runtime.
    @Ignore
    private int status = STATUS_UNKNOWN;

    public Contact() {
        this(UUID.randomUUID().toString());
    }

    private Contact(String id) {
        this.id = id;
    }

    public String getId(){
        return id;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getPathToProfilePic() {
        return pathToProfilePic;
    }

    public void setPathToProfilePic(String pathToProfilePic) {
        this.pathToProfilePic = pathToProfilePic;
    }

    public String getFirstLineDesc() {
        return displayName;
    }

    public String getSecondLineDesc() {
        return emailAddress;
    }
}

在调试com_reddragon_intouch_model_ContactRealmProxy.java类时,我发现发生异常是因为调用方法public String realmSet$id()时变量'proxyState'为空:

    public void realmSet$id(String value) {
        if (proxyState.isUnderConstruction()) { //<-- CRASH HAPPENS HERE
            // default value of the primary key is always ignored.
            return;
        }

        proxyState.getRealm$realm().checkIfValid();
        throw new io.realm.exceptions.RealmException("Primary key field 'id' cannot be changed after object was created.");
    }

这使我相信realm$injectObjectContext()中的proxyState的初始化没有被调用。

使用这种创建新对象的方法,可以与该项目中的其他两个类一起正常工作,并且我验证了realm$injectObjectContext() IS 被调用了。这是与Media.java类(有效)相同的构造类型的堆栈跟踪:

realm$injectObjectContext:105, com_reddragon_intouch_model_MediaRealmProxy (io.realm)
<init>:52, Media (com.reddragon.intouch.model)
<init>:49, Media (com.reddragon.intouch.model)
<init>:99, com_reddragon_intouch_model_MediaRealmProxy (io.realm)
newInstance:99, DefaultRealmModuleMediator (io.realm)
createObjectInternal:1048, Realm (io.realm)
createObject:1024, Realm (io.realm)
addMedia:267, MediaDataMgr (com.reddragon.intouch.model)

因此,有关Contact类的代码的生成方式必须与其他有效的方式有所不同-并且“某些情况”使Realm无法调用该方法。

该怎么办?

1 个答案:

答案 0 :(得分:0)

解决方案:

就我而言,我是Lint重构的不知情的受害者。事实证明,我在类中运行了一些Lint进程,建议的更改之一是将某些构造函数更改为私有。显然,在构造函数中采用默认args的模型类中,Realm不喜欢这样做。因此,例如Contact.java类具有以下构造函数:

public class Contact extends RealmObject implements CardListable {
    @PrimaryKey
    private String id;
    private String displayName;
    private String emailAddress;
    private String pathToProfilePic; // This will always be a URI, but we have to store it as a string and convert to URI at runtime.
    @Ignore
    private int status = STATUS_UNKNOWN;

    public Contact() {
        this(UUID.randomUUID().toString());
    }

    private Contact(String id) { //<--NOTE PRIVATE CONSTRUCTOR
        this.id = id;
    }

更改为此:

public Contact() {
    this(UUID.randomUUID().toString());
}

public Contact(String id) { //<--NOTE THIS IS NOW PUBLIC
    this.id = id;
}

并执行清理+重建解决了该问题。