NullPointerException:尝试在空对象引用上调用虚方法'void io.realm.Realm.beginTransaction()'

时间:2017-01-27 09:54:19

标签: nullpointerexception realm

所以我正在创建一个应用程序,我使用一个活动来介绍使用Realm的城市数据。我想在按下浮动操作按钮时保存数据,但我不断获得NullPointerExeption,当我对应用程序进行debbug时,我看到realm.beginTransaction()中的域是空的。

有人可以就此发生的原因提出建议吗?谢谢!

城市课程:

bbp_get_form_topic_status_dropdown_custom()

AddEditActivity:

package com.android.bianca.cityworld2.models;

import com.android.bianca.cityworld2.app.MyApplication;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.Required;

/**
 * Created by Bianca on 25/01/2017.
 */

public class City extends RealmObject {
    @PrimaryKey
    private int id;
    @Required
    private String nombre;
    @Required
    private String description;
    @Required
    private String imagen;

    public City(){
    };

    public City(String nombre, String description, String imagen) {
        this.id = MyApplication.CityID.incrementAndGet();
        this.nombre = nombre;
        this.description = description;
        this.imagen = imagen;
    }

    public int getID(){ return id;}
    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImagen() {
        return imagen;
    }

    public void setImagen(String imagen) {
        this.imagen = imagen;
    }
}

MyApplication的:

package com.android.bianca.cityworld2.activities;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.bianca.cityworld2.R;
import com.android.bianca.cityworld2.models.City;
import com.squareup.picasso.Picasso;

import io.realm.Realm;

public class AddEditActivity extends AppCompatActivity {

    private FloatingActionButton fabSave;
    private Realm realm;

    private TextView textViewNombre;
    private TextView textViewDescription;
    private ImageView imageViewImagen;
    private TextView textViewUrlFoto;
    private ImageView imageViewPreview;
    private City ciudadNueva;




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

        setContentView(R.layout.activity_add_edit);

        Realm.init(getApplicationContext());
        Realm realm = Realm.getDefaultInstance();

        bindReferences();

        imageViewPreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String imageURL = textViewUrlFoto.getText().toString();
                if(imageURL.equals("")) {
                    Toast.makeText(AddEditActivity.this, "No has introducido ningun url", Toast.LENGTH_SHORT).show();
                }else {
                    Picasso.with(AddEditActivity.this).load(imageURL).error(R.mipmap.ic_star).fit().into(imageViewImagen);
                }
            }
        });

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

                crearNuevaCiudad();
            }
        });
    }
    private void crearNuevaCiudad(){
        String nombre = textViewNombre.getText().toString().trim();
        String desc = textViewDescription.getText().toString().trim();
        String img = textViewUrlFoto.getText().toString().trim();
        ciudadNueva = new City(nombre,desc,img);

        realm.beginTransaction();
            realm.copyToRealm(ciudadNueva);
        realm.commitTransaction();

        //<Toast.makeText(AddEditActivity.this, nombre + "|"+description+"|"+img, Toast.LENGTH_SHORT).show();
    }

    private void bindReferences(){
        fabSave = (FloatingActionButton) findViewById(R.id.fabSave);
        textViewNombre =(TextView) findViewById(R.id.editTextEditAddNombre);
        textViewDescription =(TextView) findViewById(R.id.editTextAddEditDescription);
        textViewUrlFoto =(TextView) findViewById(R.id.editTextEditAddUrl);
        imageViewImagen = (ImageView) findViewById(R.id.imageViewEditAddBackground);
        imageViewPreview = (ImageView) findViewById(R.id.imageViewEditAddPreview);
    }
}

清单:

package com.android.bianca.cityworld2.app;

import android.app.Application;

import com.android.bianca.cityworld2.models.City;

import java.util.concurrent.atomic.AtomicInteger;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmObject;
import io.realm.RealmResults;

/**
 * Created by Bianca on 26/01/2017.
 */

public class MyApplication extends Application {

    public static AtomicInteger CityID = new AtomicInteger();

    @Override
    public void onCreate() {
        setUpRealmConfig();

        Realm realm = Realm.getDefaultInstance();
        CityID = getIdByTable(realm, City.class);
        realm.close();
    }

    private void setUpRealmConfig() {
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().build();
        Realm.setDefaultConfiguration(config);
    }

    private <T extends RealmObject> AtomicInteger getIdByTable(Realm realm, Class<T> anyClass) {
        RealmResults<T> results = realm.where(anyClass).findAll();
        return (results.size() > 0) ? new AtomicInteger(results.max("id").intValue()) : new AtomicInteger();
    }



}

0 个答案:

没有答案