Error: java.lang.IllegalArgumentException: column '_id' does not exist

0

Good people, I was developing an app in order to learn where the goal was to register users in an Activity and another to view all users registered through a listview. It happens that when I am viewing the users the app closes and it shows me this in logCat:

12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime: FATAL EXCEPTION: main
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime: Process: br.com.jeiferson.firstapp, PID: 2191
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.jeiferson.firstapp/br.com.jeiferson.firstapp.ListUserActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:  Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.widget.CursorAdapter.init(CursorAdapter.java:180)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.widget.CursorAdapter.<init>(CursorAdapter.java:157)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:96)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at br.com.jeiferson.firstapp.ListUserActivity.onCreate(ListUserActivity.java:38)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
12-28 22:25:49.789 2191-2191/br.com.jeiferson.firstapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

LogCat points to the error in the following class: ListUserActivity.java:38

My ListUserActivity.java

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

        ManagerDataBase crud = new ManagerDataBase(getBaseContext());
        Cursor cursor = crud.loadData();

        String[] nameField = {CreateDataBase.NAME_USER, CreateDataBase.LASTNAME_USER};
        int[] idFieldViews = new int[] {R.id.tvNameUser, R.id.tvLastNameUser};

        SimpleCursorAdapter adp = new SimpleCursorAdapter(getBaseContext(),
                R.layout.grid_layout, cursor, nameField, idFieldViews, 0);

        lvShowUser = (ListView) findViewById(R.id.lvShowUsers);
        lvShowUser.setAdapter(adp);
    }

NOTE: Disregard the rest of the code, it is not relevant!

If you need here my class ManagerDataBase.java

package br.com.jeiferson.firstapp;

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

/**
 * Created by Jeiferson de Almeida on 27/12/2015.
 */
public class ManagerDataBase {

    private SQLiteDatabase db;
    private CreateDataBase dataBase;

    public ManagerDataBase(Context context) {
        dataBase = new CreateDataBase(context);
    }

    public String insertData(String name, String lastname, String email, String phone, String age) {
        ContentValues values;
        long result;

        db = dataBase.getWritableDatabase();

        values = new ContentValues();
        values.put(CreateDataBase.NAME_USER, name);
        values.put(CreateDataBase.LASTNAME_USER, lastname);
        values.put(CreateDataBase.EMAIL_USER, email);
        values.put(CreateDataBase.PHONE_USER, phone);
        values.put(CreateDataBase.AGE_USER, age);

        result = db.insert(CreateDataBase.TABLE, null, values);
        db.close();

        if (result == -1) {
            return "Erro ao cadastrar usuario!";
        } else {
            return "Usuario cadastrado com sucesso!";
        }
    }

    public Cursor loadData() {
        Cursor cursor;

        String[] nameField = {CreateDataBase.NAME_USER, CreateDataBase.LASTNAME_USER};
        db = dataBase.getReadableDatabase();
        cursor = db.query(CreateDataBase.TABLE, nameField, null, null, null, null, null, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }

        db.close();

        return cursor;
    }
}

Could someone tell me the source of the problem?

    
asked by anonymous 28.12.2015 / 23:50

1 answer

1

In order to use a SimpleCursorAdapter it is necessary that your table has a column named _id and that it is present in the query result that the cursor represents.

The column should be declared like this:

  

_id integer primary key autoincrement

In the loadData () method, change

String[] nameField = {CreateDataBase.NAME_USER, CreateDataBase.LASTNAME_USER};

for

String[] nameField = {CreateDataBase._ID, CreateDataBase.NAME_USER, CreateDataBase.LASTNAME_USER};

Do not forget to include this constant in the CreateDataBase class

    
29.12.2015 / 00:09