This question already has an answer here:
I have a very simple app that I'm developing in Android Studio
I'm getting a NullPointerException and I don't know why. Maybe a new set of fresh eyes could pount me in the correct direction.
I'm trying to populate a listview on right after start. the data is loaded from sqlite
so, the in the mainactivity.java I have:
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Cursor cursor = myDb.getAllviaturas();
String[] fromFieldNames = new String[] {DatabaseHelper.KEY_MATRICULA, DatabaseHelper.KEY_NOME};
int[] toViewIDs = new int[] {R.id.textViewMatricula,R.id.textViewNome};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.listview_each_item,cursor,fromFieldNames,toViewIDs,0);
ListView myList = (ListView) findViewById(R.id.lvViaturas);
myList.setAdapter(myCursorAdapter);
and in the DatabseHelper.java
public Cursor getAllviaturas() {
SQLiteDatabase db = this.getReadableDatabase();
String where = null;
Cursor c = db.query(true, TABLE_VIATURAS, ALL_VIATURAS_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
Any Help?
Aucun commentaire:
Enregistrer un commentaire