gilbert111
Lurker
Hello Community,
I have a small quiz app project, that fetches the question and answer strings from a sqlite database. Most of the code I took from a book and changed it for my use, since I have no experience with this.
In the following code, the app always crashes when I open the Question activity, so when onCreate() gets called.
This is the activity code:
This is the DBHelper:
I appreciate your help!
I have a small quiz app project, that fetches the question and answer strings from a sqlite database. Most of the code I took from a book and changed it for my use, since I have no experience with this.
In the following code, the app always crashes when I open the Question activity, so when onCreate() gets called.
This is the activity code:
Code:
public class Question extends ActionBarActivity {
questionsDBHelper db;
int counter = 0;
String module;
String[][] questions = new String[questionsDBHelper.NUMBER_OF_QUESTIONS][6];
String[] answers = new String[4];
int corr;
TextView questionText;
Button answ1;
Button answ2;
Button answ3;
Button answ4;
int points = 0;
int correctAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
questionText = (TextView)findViewById(R.id.questionText);
answ1 = (Button)findViewById(R.id.answ1);
answ2 = (Button)findViewById(R.id.answ2);
answ3 = (Button)findViewById(R.id.answ3);
answ4 = (Button)findViewById(R.id.answ4);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
String destDir = "/data/data/" + getPackageName() + "/databases/";
String destPath = destDir + "quizDB";
File f = new File(destPath);
if (!f.exists()) {
//---make sure directory exists---
File directory = new File(destDir);
directory.mkdirs();
//---copy the db from the assets folder into
// the databases folder---
try {
CopyDB(getBaseContext().getAssets().open("quizDB"),
new FileOutputStream(destPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
db = new questionsDBHelper(this);
Intent receivedIntent = getIntent();
Bundle b = getIntent().getExtras();
module = b.getString("moduleCode");
questions = db.createTable(module);
}
else{
Toast toast = Toast.makeText(getApplicationContext(), "No Database found!", Toast.LENGTH_SHORT);
toast.show();
}
//First Question
questionText.setText(questions[0][0]);
answ1.setText(questions[0][1]);
answ2.setText(questions[0][2]);
answ3.setText(questions[0][3]);
answ4.setText(questions[0][4]);
correctAnswer = Integer.parseInt(questions[0][5]);
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
This is the DBHelper:
Code:
public class questionsDBHelper {
static final String TAG = "questionsDBHelper";
public static final String TABLE_NAME = "QUESTIONS";
public static final String COLUMN_NAME_ID = "ID";
public static final String COLUMN_NAME_MODULE = "MODULE";
public static final String COLUMN_NAME_QUESTION = "QUESTION";
public static final String COLUMN_NAME_ANSWER_1 = "ANSWER_1";
public static final String COLUMN_NAME_ANSWER_2 = "ANSWER_2";
public static final String COLUMN_NAME_ANSWER_3 = "ANSWER_3";
public static final String COLUMN_NAME_ANSWER_4 = "ANSWER_4";
public static final String COLUMN_NAME_CORRECT = "CORRECT";
private static final String SQL_CREATE = "CREATE TABLE" + TABLE_NAME + "("
+ COLUMN_NAME_ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_NAME_MODULE + "TEXT NOT NULL," + COLUMN_NAME_QUESTION
+ "TEXT NOT NULL," + COLUMN_NAME_ANSWER_1 + COLUMN_NAME_ANSWER_1
+ "TEXT NOT NULL," + COLUMN_NAME_ANSWER_2 + "TEXT NOT NULL,"
+ COLUMN_NAME_ANSWER_3 + "TEXT NOT NULL," + COLUMN_NAME_ANSWER_4
+ "TEXT NOT NULL," + COLUMN_NAME_CORRECT + "TEXT NOT NULL);";
private static final String SQL_DELETE = "DROP TABLE IF EXISTS "
+ TABLE_NAME;
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "quizDB";
public static final int NUMBER_OF_QUESTIONS = 3;
SQLiteDatabase db;
DatabaseHelper DBHelper;
final Context context;
public questionsDBHelper(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(SQL_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS QUESTIONS");
onCreate(db);
}
}
public questionsDBHelper open() throws SQLException{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public Cursor getRandomQuestion(String module) {
int i;
Random r = new Random();
Cursor mCursor = db.query(true, TABLE_NAME, new String[] {
COLUMN_NAME_ID, COLUMN_NAME_MODULE, COLUMN_NAME_ANSWER_1,
COLUMN_NAME_ANSWER_2, COLUMN_NAME_ANSWER_3,
COLUMN_NAME_ANSWER_4, COLUMN_NAME_CORRECT }, COLUMN_NAME_MODULE
+ "=" + module, null, null, null, null, null);
if (mCursor != null) {
i = mCursor.getCount();
mCursor.moveToFirst();
mCursor.move(r.nextInt(i-1));
}
return mCursor;
}
public String[][] createTable(String module){
String[][] roundQuestions = new String[NUMBER_OF_QUESTIONS][6];
for(int i = 0; i < NUMBER_OF_QUESTIONS; i++){
roundQuestions[i][0] = getRandomQuestion(module).getString(2);
roundQuestions[i][1] = getRandomQuestion(module).getString(3);
roundQuestions[i][2] = getRandomQuestion(module).getString(4);
roundQuestions[i][3] = getRandomQuestion(module).getString(5);
roundQuestions[i][4] = getRandomQuestion(module).getString(6);
roundQuestions[i][5] = Integer.toString(getRandomQuestion(module).getInt(7));
}
return roundQuestions;
}
}
I appreciate your help!