null对象引用上的checkIfValid()(Realm 3.1.3)

时间:2017-05-14 08:56:11

标签: android realm

我在项目中使用了Realm 3.1.3和Realm Adapters 2.0,当我尝试使用

更新适配器时出现此错误
adapter.updateData(RealmManager.stationsDetailsDAO().loadSearch(newText.toLowerCase(), selectedLanguage, type));

这是抛出的错误

Attempt to invoke virtual method 'void io.realm.BaseRealm.checkIfValid()' on a null object reference

尝试从数据库查询时

这是我的代码:

public RealmList<Station> loadSearch(String query, String language, String type) {

    RealmList<Station> stationRealmList = new RealmList<>();

    mRealm.executeTransaction(realm -> mRealm.copyToRealm(stationRealmList)); 
    // To make it a Managed Object

    switch (type) {
        case "Language": {
            RealmQuery<Station> stationRealmQuery = mRealm.where(Station.class);
            stationRealmQuery.equalTo("language", language);
            RealmResults<Station> realmResults = stationRealmQuery.findAll();
            for (int i = 0; i < realmResults.size(); i++) {
                if (realmResults.get(i).getName().contains(query)) {
                    stationRealmList.add(realmResults.get(i));
                }

            }

            return stationRealmList;
        }
        case "Recent":
            HeaderSectionModel realmList = RealmManager.stationsDetailsDAO().loadHeaderData();
            for (int i = 0; i < realmList.getLanguageModelArrayList().size(); i++) {
                if (realmList.getLanguageModelArrayList().get(i).getName().contains(query)) {
                    stationRealmList.add(realmList.getLanguageModelArrayList().get(i));
                }
            }

            return stationRealmList;
        case "Genre": {
            RealmQuery<Station> stationRealmQuery = mRealm.where(Station.class);
            stationRealmQuery.equalTo("genreName", language);
            RealmResults<Station> realmResults = stationRealmQuery.findAll();


            for (int i = 0; i < realmResults.size(); i++) {
                if (realmResults.get(i).getName().contains(query)) {
                    stationRealmList.add(realmResults.get(i));
                }
            }

            return stationRealmList;
        }
    }


    return stationRealmList;
}

1 个答案:

答案 0 :(得分:1)

因为RealmList<Station> stationRealmList = new RealmList<>();不是链接的托管视图(如未使用getRealmListField() 从托管的RealmObject获取),所以RealmRecyclerViewAdapter 没有&使用它。

解决方案是,您应该显示由RealmResults定义的RealmQuery,而不是手动尝试执行包含操作。

public RealmResults<Station> loadSearch(String query, String language, String type) { 
    RealmQuery<Station> stationRealmQuery = mRealm.where(Station.class);
    if(query != null && !"".equals(query)) {
        stationRealmQuery.contains("name", query);
    }
    switch (type) {
        case "Language": 
            stationRealmQuery.equalTo("language", language);
            break;
        case "Recent":
            // TODO do something about this one
            break;
        case "Genre": 
            stationRealmQuery.equalTo("genreName", language);
            break;
    }
    return stationRealmQuery.findAll();
}

如果无法做到这一点,请使用常规RecyclerView.Adapter和您自己的RealmChangeListener