IllegalArgumentException error while displaying in-app data

0

I'm trying to display everything that was written to my app at runtime, but it displays the error below:

  

Unable to start activity ComponentInfo {com.project ....}:   java.lang.IllegalArgumentException: column '_id' does not exist

BooksActivity:

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 that 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);
}
}
    
asked by anonymous 21.07.2016 / 02:08

1 answer

1

I think your error is here

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

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

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

I think the book field can not be changed, once it is created it opens the Android Device Monitor and looks at the structure of your table to see if it is correct, believe that the fields are with the right name to sqlite path in Android Device Monitor would be

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

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

    
25.07.2016 / 03:25