如何从几个EDITTEXT的sqlite表加载数据

时间:2016-11-03 16:43:44

标签: android sqlite ormlite

我无法获取此数据并显示在某个活动上,有人可以帮助我吗?我如何调用DAO并传递此数据?

到目前为止用于解决此问题的所有类跟踪。

班级模特

 @DatabaseTable(tableName = "pessoa")
public class Pessoa implements EntidadePersitivel {
    @DatabaseField(generatedId = true)
     private int id;
    @DatabaseField
     private int codigo;
    @DatabaseField
     private  String nome;
    @DatabaseField
     private String cnpjCpf;


    public void setUf(String uf) {
        this.uf = uf;
    }

    @DatabaseField
     private String rgIE;
    @DatabaseField
     private String cep;
     @DatabaseField
    private String uf;
    @DatabaseField
     private String cidade;
    @DatabaseField
     private String bairro;
    @DatabaseField
     private String endereco;
    @DatabaseField
     private  String numero;
    @DatabaseField
     private String complemento;
    @DatabaseField
     private  String foneComercial;
    @DatabaseField
     private  String foneResidencial;
    @DatabaseField
     private  String foneCelular;
    @DatabaseField
     private  String email;


    public Pessoa(int id, int codigo, String nome, String cnpjCpf, String rgIE,
                  String cep, String cidade, String bairro, String endereco,
                  String numero, String complemento, String foneComercial, String foneResidencial, String foneCelular, String email) {
        this.id = id;
        this.codigo = codigo;
        this.nome = nome;
        this.cnpjCpf = cnpjCpf;
        this.rgIE = rgIE;
        this.cep = cep;

        this.cidade = cidade;
        this.bairro = bairro;
        this.endereco = endereco;
        this.numero = numero;
        this.complemento = complemento;
        this.foneComercial = foneComercial;
        this.foneResidencial = foneResidencial;
        this.foneCelular = foneCelular;
        this.email = email;
    }

    public Pessoa() {

    }

类DAO

  */

public class PessoaDao extends BaseDaoImpl<Pessoa, Integer> {
    public PessoaDao(ConnectionSource connectionSource) throws SQLException {
        super(Pessoa.class);
        setConnectionSource(connectionSource);
        initialize();


    }

}

class dataBaseHelper

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String dataBaseName= "central";
    private static final int version =1;

    public DatabaseHelper(Context context) {
        super(context, dataBaseName, null, version);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {

        try {
            TableUtils.createTable(connectionSource,LoginSeralizable.class);
            TableUtils.createTable(connectionSource, Pessoa.class);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource, LoginSeralizable.class,true);
            TableUtils.dropTable(connectionSource, Pessoa.class, true);

            onCreate(sqLiteDatabase, connectionSource);
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }

    }

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

错误日志

致命例外:Thread-284

                                                                         Process: routerbox.com.br.centraisdoassinante, PID: 25202
                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object routerbox.com.br.centraisdoassinante.routerbox.com.br.centraisdoassinante.Dao.PessoaDao.queryForId(java.lang.Object)' on a null object reference
                                                                              at routerbox.com.br.centraisdoassinante.DadosCadastrais$1.run(DadosCadastrais.java:49)
                                                                              at java.lang.Thread.run(Thread.java:818)

2 个答案:

答案 0 :(得分:1)

  

java.lang.NullPointerException:尝试调用虚方法'java.lang.Object routerbox.com.br.centraisdoassinante.routerbox.com.br.centraisdoassinante.Dao.PessoaDao.queryForId(java.lang.Object)'on空对象引用

您正在尝试在null对象中执行方法queryForId,这是什么意思?

PessoaDao是一个非静态类,这意味着您需要在使用它们之前对其进行实例化。

所以你删除了你调用这个函数的代码部分,但是如果我没记错的话你有类似的东西:

PessoaDao.queryForId('1');

但你需要这样做:

PessoaDao pessoaDao = new PessoaDao(yourConnectionSource);
pessoaDao.queryForId('1');
是的,我认为Pessoa是葡萄牙语吗?也许你可以在Stack Overflow in portuguese询问,这样我可以更好地回答你,你不需要处理我的坏英语:)

答案 1 :(得分:0)

我喜欢卢卡斯说过,我引用了连接并管理了

protected void onCreate(Bundle savedInstanceState)  {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dados_cadastrais);
            databaseHelper = new DatabaseHelper(DadosCadastrais.this);

            new Thread(new Runnable() {
                @Override
                public void run() {

                    try {
                        pessoaDao = new PessoaDao(databaseHelper.getConnectionSource());
                        pessoa= pessoaDao.queryForId(1);
                        edtNome= (EditText) findViewById(R.id.edtNome);
                        edtNome.setText(pessoa.getNome());

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }


                }
            }).start();



    }