【问题标题】:Problem with creating realm object ( IllegalStateException: Cannot modify managed objects outside of a write transaction)创建领域对象的问题(IllegalStateException:无法在写入事务之外修改托管对象)
【发布时间】:2019-04-07 10:32:12
【问题描述】:

我在向 Realm 数据库添加新对象时遇到了问题。 我在 Main Activity 中有 Product 类扩展 Realm Object 和我的主要代码。当我在没有

的情况下启动应用程序时

Product product1 = myRealm.createObject(Product.class);

输入的对象被添加(出现在屏幕上)到领域列表,当我去另一个活动时它们消失了(也是一个问题,但不是这种情况)。 我在 Logcat 中看到了该代码字符串的引用。 "java.lang.IllegalStateException: 无法在写事务之外修改托管对象。 "

product也是一个字符串,所以现在你明白什么意思了

setProduct.

我添加的时候也有问题

myRealm.commitTransaction()

什么的。

  public class MainActivity extends AppCompatActivity {
static View view1;
EditText editText;
static RealmList<Product> productRealmList;
static Realm myRealm = Realm.getDefaultInstance();

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Product TV = new Product("TV");
    Product watch = new Product("Watch");

    productRealmList = new RealmList<>();
    productRealmList.add(TV);
    productRealmList.add(watch);

    MyAdapter adapter = new MyAdapter(this, R.layout.adapter_layout, productRealmList);
    listView.setAdapter(adapter);


    findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_layout, null);
            editText = view1.findViewById(R.id.ent);
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Create new product")
                    .setMessage("Put down the name of the new product")
                    .setView(view1)
                    .setNegativeButton("Cancel", null)
                    .setPositiveButton("Add",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
  Product product1 = myRealm.createObject(Product.class);
                                        product1.setProduct(editText.getText().toString());
productRealmList.add(product1);

                                }
                            })

                    .create()
                    .show();
        }
    });

当用户输入他/她的产品名称并在警报对话框中按添加时,我想将产品保存到数据库和 RealmList。 (希望它们显示在屏幕上)

【问题讨论】:

    标签: java android realm


    【解决方案1】:

    您需要在事务中创建您的RealmObject,因为异常也会告诉您。

    看看:https://realm.io/docs/java/latest/#transaction-blocks

    在您的OnClickListener 中执行以下操作:

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            final Product product1 = myRealm.createObject(Product.class);
            product1.setProduct(editText.getText().toString());
            productRealmList.add(product1);
        }
    });
    

    您可能还必须在事务中创建productRealmList,或者至少从事务块中的 Realm 获取它,但是如果不使用 Realm 创建一个全新的项目,我很难进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多