1

我正在尝试在我的项目中实现 Realm 数据库,我有一个带有在数据库中添加项目的方法的类,我使用该类来存储这些方法并在我的片段中调用它们,但是当我这样做时,我得到一个错误:

java.lang.NullPointerException: Attempt to invoke interface method 'void io.realm.RealmChangeListener.onChange()' on a null object reference

我在这里做错了什么?

public class Repositorio extends GlobalActivity {


    private RealmChangeListener realmListener;
    public Realm realm;
    private Context context;



    public Repositorio() {
    }

    public Repositorio(Context context) {
        this.context = context;
        this.realm = Realm.getDefaultInstance();
        this.realmListener = new RealmChangeListener() {
            @Override
            public void onChange() {
                Log.i("realmtest", "some values in the database have been changed");
            }
        };

        this.realm.addChangeListener(realmListener);
    }


    public void addCliente(String nome, String local, String estado, String telefone, String email, String tipo) {
        realm.beginTransaction();

        Pessoa mPessoa = realm.createObject(Pessoa.class);
        mPessoa.setId(UUID.randomUUID().toString());
        mPessoa.setNome(nome);
        mPessoa.setCidade(local);
        mPessoa.setEstado(estado);
        mPessoa.setTelefone(telefone);
        mPessoa.setEmail(email);
        mPessoa.setTipo(tipo);

        realm.commitTransaction();


        Toast.makeText(context, "Cliente adicionado com sucesso", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onPause() {
        super.onPause();

        realm.removeChangeListener(realmListener);

    }

    @Override
    public void onResume() {
        super.onResume();

        realm.addChangeListener(realmListener);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (realm != null) {
            realm.close();
            realm = null;
        }
    }


}

分段:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View v = inflater.inflate(R.layout.cadastra_produtos, container, false);

        mRepositorio = new Repositorio(getActivity());

        novocliente = (TextView) v.findViewById(R.id.novocliente);



        novocliente.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mRepositorio.addCliente("ZÉ", "LALAAL", "SC", "3333-3333", "teste@teste.com", "Pessoa Física");
            }
        });

        return v;
    }

全球的:

public class GlobalActivity extends AppCompatActivity {

    public Ferramentas mFerramentas;
    public Realm realm;


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


        mFerramentas = new Ferramentas();
        realm = Realm.getDefaultInstance();



    }


}
4

1 回答 1

1

Android 框架Repositorio使用默认构造函数(公共且无参数)实例化活动。

的默认构造函数Repositorio不将侦听器实例分配给realmListener字段。

所以,当onResume()被调用时,realm.addChangeListener(realmListener);等价于realm.addChangeListener(null);

您不应定义默认构造函数以外的构造函数。而且你不应该手动实例化 Activity(Repositorio)。

于 2016-05-10T22:06:48.197 回答