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

Aula 4x05 - Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 7 columns.

E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 7 columns. D/AndroidRuntime: Shutting down VM

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: br.com.alura.agenda, PID: 2349 java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.alura.agenda/br.com.alura.agenda.ListaAlunosActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetString(Native Method) at android.database.CursorWindow.getString(CursorWindow.java:438) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) at br.com.alura.agenda.dao.AlunoDAO.buscaAlunos(AlunoDAO.java:61) at br.com.alura.agenda.ListaAlunosActivity.onCreate(ListaAlunosActivity.java:24) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Application terminated.

3 respostas

Vinicius, cola teu código da ListaAlunosActivity e do AlunoDAO

ListaAlunosActivity

package br.com.alura.agenda;

import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView;

import java.util.List;

import br.com.alura.agenda.dao.AlunoDAO; import br.com.alura.agenda.modelo.Aluno;

public class ListaAlunosActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lista_alunos);

AlunoDAO dao = new AlunoDAO(this); List alunos = dao.buscaAlunos(); dao.close();

ListView listaAlunos = (ListView) findViewById(R.id.lista_alunos); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, alunos); listaAlunos.setAdapter(adapter);

Button novoAluno = (Button) findViewById(R.id.novo_aluno); novoAluno.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intentVaiProFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class); startActivity(intentVaiProFormulario); } }); } }

AlunoDAO

package br.com.alura.agenda.dao;

import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList; import java.util.List;

import br.com.alura.agenda.modelo.Aluno;

/* Created by ViníciusA on 16/11/2016. */

public class AlunoDAO extends SQLiteOpenHelper { public AlunoDAO(Context context) { super(context, "Agenda", null, 1); }

@Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, sobrenome TEXT, sexo TEXT, endereco TEXT, site TEXT, nota REAL );"; db.execSQL(sql); }

@Override public void onUpgrade(SQLiteDatabase db, int i, int i1) { String sql = "DROP TABLE IF EXISTS Alunos"; db.execSQL(sql); onCreate(db); }

public void insere(Aluno aluno) { SQLiteDatabase db = getWritableDatabase();

ContentValues dados = new ContentValues(); dados.put("nome", aluno.getNome()); dados.put("sobrenome", aluno.getSobrenome()); dados.put("sexo", aluno.getSexo()); dados.put("endereco", aluno.getEndereco()); dados.put("site", aluno.getSite()); dados.put("nota", aluno.getNota());

db.insert("Alunos", null, dados); }

public List buscaAlunos() { String sql = "SELECT * FROM Alunos;"; SQLiteDatabase db = getReadableDatabase(); Cursor c = db.rawQuery(sql, null);

List alunos = new ArrayList(); while (c.moveToNext()) { Aluno aluno = new Aluno(); aluno.setId(c.getLong(c.getColumnIndex("id"))); aluno.setNome(c.getString(c.getColumnIndex("nome"))); aluno.setSobrenome(c.getString(c.getColumnIndex("sobrenome"))); aluno.setSexo(c.getString(c.getColumnIndex("sexonome"))); aluno.setEndereco(c.getString(c.getColumnIndex("endereco"))); aluno.setSite(c.getString(c.getColumnIndex("site"))); aluno.setNota(c.getDouble(c.getColumnIndex("nota")));

alunos.add(aluno); } // Avisamos o cursor que já pode fechar a conexão para liberar a memória c.close();

return alunos; } }

solução!

Olá Vinicios, da uma olhada nessa sua linha do buscaAluno aluno.setSexo(c.getString(c.getColumnIndex("sexonome")));

Esse campo sexonome não existe na sua tabela e quando tenta acessá-lo da seu erro. Mude para apenas sexo que deve resolver.