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 be able to view all registered users through a listview. It turns out that when I go to view the users the app closes and presents it to me 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 out 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 my Managerdatabase class here.
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;
}
}
Would anyone know the source of the problem?
Thanks ramaral, solved my problem!
– Jeiferson