片段中的领域导致 NullPointerException

Realm in Fragment causing NullPointerException

当片段的 activity 扩展了控制领域的基础 activity 时,我对如何在片段中处理领域感到困惑。

换句话说,我有这个结构:

Base Activity -> User Activity -> Base Fragment -> Fragment

一切正常,直到我退出 activity(即返回到之前的 activity)。我收到此错误:

java.lang.RuntimeException: Unable to destroy activity {UserActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.realm.Realm.close()' on a null object reference

...

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.realm.Realm.close()' on a null object reference at BaseFragment.onDestroyView

我的基地Activity:

public abstract class BaseActivity extends AppCompatActivity {
    private Realm realm;

    protected abstract int getLayoutResource();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());

        realm = Realm.getDefaultInstance();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close();
    }

    public Realm getRealm() {
        return realm;
    }
}

我的主要Activity(创建片段的地方):

public class UserActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String type = getIntent().getStringExtra("type");

        Fragment fragment = null;
        if (findViewById(R.id.fragment_container) != null) {
            if (savedInstanceState != null) {
                return;
            }

            if (type.equals("first")) {
                fragment = new FirstFragment();
            } else if (type.equals("second")) {
                fragment = new SecondFragment();
            }

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, fragment).commit();
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_user;
    }
}

我所有的片段都扩展了一个基础片段。这是我的基本片段:

public abstract class BaseFragment extends Fragment {

    private Realm realm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        realm = Realm.getDefaultInstance();

        return inflater.inflate(getFragmentLayout(), container, false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        realm.close();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        realm.executeTransaction(new Realm.Transaction() {
            @Override
                public void execute(Realm realm) {
                    // some realm operation
                }
        });
    }

    protected abstract int getFragmentLayout();
}

导致问题的原因是什么?我是不是对 Realm 管理不当?

去掉你的BaseFragment中的realm.close();,它会在你销毁activity时关闭,所以你将关闭它两次,这就是为什么你得到了 NPE。因此,如果您删除 Fragment 中的 realm.close();,并让 Activity 处理关闭 Realm