Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Dúvida Aula 5 - Aplicação não inicializa

Erro The application CadastrAluno (process br.com.cadastro) has stopped unexpectedly. Please try again.

Classe AlunoDAO


public class AlunoDAO extends SQLiteOpenHelper {

    private static final String DATABASE = "NomeDoBanco" ;
    private static final int VERSAO = 1;
    private static final String TABELA = "Alunos";

    public AlunoDAO(Context ctx) {
        super(ctx, DATABASE, null, VERSAO);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String sql = "CREATE TABLE " + TABELA + " ("
                + "id INTEGER PRIMARY KEY, "
                + "nome TEXT UNIQUE NOT NULL, "
                + "telefone TEXT, "
                +"endereco TEXT, "
                +"site TEXT, "
                +"nota REAL, "
                +"caminhoFoto TEXT"
                +");";

     database.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + TABELA + ";";
        database.execSQL(sql);
        onCreate(database);

    }

    public void insere(Aluno aluno) {
        ContentValues cv = new ContentValues();
        cv.put("nome", aluno.getNome());
        cv.put("site", aluno.getSite());
        cv.put("endereco", aluno.getEndereco());
        cv.put("telefone", aluno.getNota());
        cv.put("caminhoFoto", aluno.getCaminhoFoto());

        getReadableDatabase().insert(TABELA, null, cv);


    }

    public List<Aluno> getLista() {

        ArrayList<Aluno> alunos = new ArrayList<Aluno>();

        String sql= "SELECT * FROM " + TABELA + ";";
        Cursor c = getReadableDatabase().rawQuery(sql, null);

        while(c.moveToNext()){
            Aluno aluno = new Aluno();
            aluno.setId(c.getLong(c.getColumnIndex("id")));
            aluno.setNome(c.getString(c.getColumnIndex("nome")));
            aluno.setSite(c.getString(c.getColumnIndex("site")));
            aluno.setEndereco(c.getString(c.getColumnIndex("endereco")));
            aluno.setTelefone(c.getString(c.getColumnIndex("telefone")));
            aluno.setNota(c.getDouble(c.getColumnIndex("nota")));
            aluno.setCaminhoFoto(c.getString(c.getColumnIndex("caminhoFoto")));

            alunos.add(aluno);
        }
        return null;
    }

}
3 respostas

ListarAlunoActivity


public class ListaAlunoActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listagem_alunos);

        ListView lista = (ListView) findViewById(R.id.lista);


        AlunoDAO dao = new AlunoDAO(this);

        List<Aluno> alunos = dao.getLista();

        ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos);

        lista.setAdapter(adapter);

        lista.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(ListaAlunoActivity.this, "a posição é " + posicao, Toast.LENGTH_SHORT).show();
            }

        });

        lista.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> adapter, View view, int posicao, long id) {
                Toast.makeText(ListaAlunoActivity.this, "Aluno clicado é " + adapter.getItemIdAtPosition(posicao),
                        Toast.LENGTH_SHORT).show();

                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_lista_alunos, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.novo:
        Intent irParaFormulario =    new Intent(this, FormularioActivity.class);
        startActivity(irParaFormulario);

            break;

        default:
            break;
        }

        return super.onOptionsItemSelected(item);
    }
}
solução!

Oi Davi, tudo bem ?

Davi seu código tá bem legal, a única coisa que você precisa arrumar é no seu DAO, o método getLista(), o retorno dele está como null, então quando você tenta usa-lo na activity você leva uma exception !

Troca o retorno e fala para gente se aconteceu mais algum erro por favor !

Espero ter ajudado !

Abraços :)

Realmente Matheus Brandino , o retorno null estava quebrando o código, obrigado pela ajuda! Abraço!!!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software