0
Day number 3. The app insists on "crashing" its startup and the Frustration is strong at the moment. The problem, I believe, is found in the getFromDb() method, more specifically in the line where I call the db.getReadableDatabase() method since the app runs NORMALLY without the stretch 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;
}
}
Can anyone please tell me what’s wrong? If needed I can add more information later.
Ask the question the error log.
– ramaral
Great Ramaral, by the way! I read the error log and the problem was that the getFromDb() method was being called for an unspecified Arraylist object in main_activity... You won.
– Pedro H.R.O.G.