Illegalargumentexception error when displaying data in the application

Asked

Viewed 101 times

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);
}
}
  • 1

    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!

  • @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..

  • 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) )

  • Uninstall the app from your device and run it again that resolves, or change the version of your local database.

  • @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.

  • @Gabrielhenrique No method carregaDados() has the array of fields, which returns in that part the command banco.id?

  • @Igormello is being recovered the value of the id variable: public Static String id = "id";

  • Best via chat to avoid getting too long comments -> http://chat.stackexchange.com/rooms/42919/discussion

Show 3 more comments

1 answer

1

I think your mistake is here

  String sql = "CREATE TABLE "+tabela+"( "
        +id+" integer primary key autoincrement, "
        +livro+" text(40)"
        +")";

by which I noticed your id and a variable as this field should not be changed you can change it by _id , something else every time you start this project onCreate will create a new table if you want to avoid this use if exist, so the table will only be created once , try using this query here;

 String sql = "create table if not exist "+tabela+"( _id integer primary key autoincrement,livro text(40))";

believe q the book field tbm can not be changed , after created opens the Android Device Monitor and looks at the structure of your table to check if it is correct , have Crete that the fields are with the right name the path of the sqlite in Android Device Monitor would be

 /data/data/com.package.appname/databases

PS: I think that would be your problem , you would have to have more details of your code to know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.