java.lang.IllegalStateException:Realm不再管理对象。它被删除了吗?

时间:2016-05-04 13:12:30

标签: android android-recyclerview realm

我正在学习Realm和Recyclerview。我做了一个简单的应用程序,你可以简单地添加项目并删除它们一切正常,直到我开始逐个删除项目。在我点击删除的最后一项,它给我异常

java.lang.IllegalStateException: Object is no longer managed by Realm. Has it been deleted?

这是我的代码

  @Override
    public void onDeleteClick(int position) {
        mRealm.beginTransaction();
        mResult.get(position).deleteFromRealm();
        mRealm.commitTransaction();
        adapter.notifyItemRemoved(position);
    }

这是完整堆栈

  java.lang.IllegalStateException: Object is no longer managed by Realm. Has it been deleted?
                                                                           at io.realm.internal.InvalidRow.getStubException(InvalidRow.java:192)
                                                                           at io.realm.internal.InvalidRow.getString(InvalidRow.java:88)
                                                                           at io.realm.PersonsRealmProxy.realmGet$firstName(PersonsRealmProxy.java:67)
                                                                           at techiespk.realmpractice.Persons.getFirstName(Persons.java:22)
                                                                           at techiespk.realmpractice.MyListAdapter.onBindViewHolder(MyListAdapter.java:44)
                                                                           at techiespk.realmpractice.MyListAdapter.onBindViewHolder(MyListAdapter.java:15)
                                                                           at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
                                                                           at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
                                                                           at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
                                                                           at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
                                                                           at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
                                                                           at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
                                                                           at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
                                                                           at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                           at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
                                                                           at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
                                                                           at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)

1 个答案:

答案 0 :(得分:9)

好的,感谢Christian Melchior的帮助。我通过这个来修复我的代码

   @Override
    public void onBindViewHolder(MyListViewHolder holder, int position) {
            Persons p = persons.get(position);
            holder.firstName.setText(p.getFirstName());
            holder.lastName.setText(p.getLastName());
    }

   @Override
    public void onBindViewHolder(MyListViewHolder holder, int position) {

        if (persons.get(position).isValid()) {
            Persons p = persons.get(position);
            holder.firstName.setText(p.getFirstName());
            holder.lastName.setText(p.getLastName());
        }
    }