Android Realm和片段中的空对象引用

时间:2018-08-26 21:09:38

标签: android realm

我正在寻找问题的答案。这是我第一次接触Realm数据库,并且在保存数据时出现片段中空对象引用的问题。有人知道我在做什么错吗?

Helper方法:

public void saveTask(final String text, final String date, final String time ) {
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            Task task = bgRealm.createObject(Task.class);
            task.setText(text);
            task.setDate(date);
            task.setTime(time);
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            // Transaction was a success.
            Log.d(TAG, "ON_SUCCESS: Data Written Successfully");
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            // Transaction failed and was automatically canceled.
            Log.d(TAG, "ON_ERROR: Error");
        }
    });

}

片段中的OnClick方法:

@OnClick(R.id.save_button)
public void onClickSave() {

    realmHelper.saveTask(title_text.getText().toString().trim(), date_text.getText().toString().trim(), time_text.getText().toString().trim());
}

onCreateView中的代码:

    ButterKnife.bind(this, view);

    ((MainActivity) getActivity()).hideFloatingActionButton();

    Realm.init(getActivity());
    RealmConfiguration configuration = new RealmConfiguration
            .Builder()
            .deleteRealmIfMigrationNeeded()
            .build();
    realm = Realm.getInstance(configuration);

我尝试了onClick方法中具有textview字段的解决方案,但仍然相同。

1 个答案:

答案 0 :(得分:1)

在片段的OnClick方法中,我观察到您有一个领域帮助器类,该类创建了一个名为realmHelper的实例。由于您异步使用领域,因此建议您将实例化的领域对象传递给helper方法,或者在helper方法或类中重新实例化领域。

将领域实例传递给helper方法

public void saveTask(Realm realm, final String text, final String date, final String time ) {
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            Task task = bgRealm.createObject(Task.class);
            task.setText(text);
            task.setDate(date);
            task.setTime(time);
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            // Transaction was a success.
            Log.d(TAG, "ON_SUCCESS: Data Written Successfully");
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            // Transaction failed and was automatically canceled.
            Log.d(TAG, "ON_ERROR: Error");
        }
    });

}

使用助手方法实例化领域

public void saveTask(Context context, final String text, final String date, final String time ) {
    Realm.init(context);
    RealmConfiguration configuration = new RealmConfiguration
        .Builder()
        .deleteRealmIfMigrationNeeded()
        .build();
    Realm realm = Realm.getInstance(configuration);
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            Task task = bgRealm.createObject(Task.class);
            task.setText(text);
            task.setDate(date);
            task.setTime(time);
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            // Transaction was a success.
            Log.d(TAG, "ON_SUCCESS: Data Written Successfully");
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            // Transaction failed and was automatically canceled.
            Log.d(TAG, "ON_ERROR: Error");
        }
    });

}