Day number 3. The app insists on "crashing" on startup and the frustration is strong at the moment. The problem, I believe, lies with the getFromDb () method, more specifically on the line where I call the db.getReadableDatabase () since the app runs NORMALLY without the excerpt between "/ * * /" highlighted in the code below.
Follow the problematic code.
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "productsManager";
private static final String TABLE_PRODUCTS = "products";
private static final String _PRODUCT = "product";
public DatabaseHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE products (id INTEGER PRIMARY KEY AUTO_INCREMENT, product TEXT )";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
public void addToDb(String product){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues valor = new ContentValues();
valor.put(_PRODUCT, product);
db.insert(TABLE_PRODUCTS, null, valor);
db.close();
}
public ArrayList<String> getFromDb(){
ArrayList<String> stringList = new ArrayList<String>();
/* SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_PRODUCTS, null, null, null, null, null, null);
if(cursor.getCount() > 0){
cursor.moveToFirst();
do{
stringList.add(cursor.getString(1));
}while(cursor.moveToNext());
}
cursor.close();*/
return stringList;
}
}
Please anyone know what is wrong? If needed I can add more information later.