0
I’m trying to present everything that was recorded in my app while running, however it presents the error below:
Unable to start Activity Componentinfo{com.projecto.... }: java.lang.Illegalargumentexception: column '_id' does not exist
Livrosactivity:
public class LivrosActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_livros);
ListView lista;
BancoControlador control = new BancoControlador(getBaseContext());
Cursor cursor = control.carregaDados();
String[] nomeCampos = new String[]{CriaBanco.id, CriaBanco.livro};
int[] idViews = new int[] {R.id.idLivro, R.id.nomeLivro};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.activity_main, cursor, nomeCampos, idViews, 0);
lista = (ListView) findViewById(R.id.listViewLivros);
lista.setAdapter(adapter);
}}
Method to assign values:
public Cursor carregaDados(){
Cursor cursor;
String[] campos = {banco.id,banco.livro};
db = banco.getReadableDatabase();
cursor = db.query(banco.tabela, campos, null, null, null, null, null);
if(cursor!=null){
cursor.moveToFirst();
}
db.close();
return cursor;
}
XML:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewLivros"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2" >
<TextView
android:id="@+id/idLivro"
android:layout_width="0dip"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="ID" />
<TextView
android:id="@+id/nomeLivro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="Livro" />
</GridLayout>
Oncreate method - Onupgrade:
public CriaBanco(Context context) {
super(context, nomebanco, null, versao, null);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE "+tabela+"( "
+id+" integer primary key autoincrement, "
+livro+" text(40)"
+")";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+tabela);
onCreate(db);
}
}
Error occurs because you did not find the field _id in the table! Could put the code that creates the table, and the method loaded() ? It would help so much! Thank you!
– Thiago Luiz Domacoski
@Thiagoluizdomacoski The loaded methodDados is between Activity and XML.. I am informing the ID when declaring the vector String fields[], if you want, I can edit the post with the code of the other classes..
– Gabriel Henrique
In his Sqliteopenhelper has a method public void onCreate(final Sqlitedatabase db) that creates the tables in the database, right? see if when you create the table book you declare the _id sort of like this : CREATE TABLE book ( _id INTEGER PRIMARY KEY, NAME TEXT) )
– Thiago Luiz Domacoski
Uninstall the app from your device and run it again that resolves, or change the version of your local database.
– Leonardo Dias
@Thiagoluizdomacoski I performed the two procedures above, but none solved.. I edited my question with the onCreate method.. I am able to register normally, but the problem is at the time of loading the data.
– Gabriel Henrique
@Gabrielhenrique No method
carregaDados()
has thearray
of fields, which returns in that part the commandbanco.id
?– Igor Mello
@Igormello is being recovered the value of the id variable: public Static String id = "id";
– Gabriel Henrique
Best via chat to avoid getting too long comments -> http://chat.stackexchange.com/rooms/42919/discussion
– Igor Mello