title: Android getDatabase called recursively error
body: <p>Hi I am trying to populate a database with a series of questions, but I can't get tables to be populated by the questions to retrieve into the app. I'm getting this error(full error log attached):</p>
  
  <p>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hp.demo/com.example.hp.demo.MainActivityQuizBasics}: java.lang.IllegalStateException: getDatabase called recursively</p>
  
  <p><a href="https://i.stack.imgur.com/AJNIH.png" rel="nofollow noreferrer">error log</a></p>
  
  <p>I have been trying for the past hour to try and resolve this but I can't work out how to fix this. I know it's to do with calling the getWritableDatabase functions in the addQuestions methods but I don't know what to do to fix it. If anyone has any suggestions that would be much appreciated.</p>
  
  <p><strong>MainActivityQuizBasics</strong></p>
  
  <pre><code>package com.example.hp.demo;
  
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.CountDownTimer;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ProgressBar;
  import android.widget.RadioButton;
  import android.widget.RadioGroup;
  import android.widget.TextView;
  
  import com.example.hp.demo.DbHelper.DatabaseHelper;
  import com.example.hp.demo.Model.QuestionBasics;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Random;
  import java.util.concurrent.TimeUnit;
  
  public class MainActivityQuizBasics extends AppCompatActivity {
  public int score = 0;
  public ArrayList&lt;String&gt; wrongQuestListBasics = new ArrayList&lt;String&gt;();
  public ArrayList&lt;String&gt; selectedBasics = new ArrayList&lt;String&gt;();
  public ArrayList&lt;String&gt; actualAnswerBasics = new ArrayList&lt;String&gt;();
  List&lt;QuestionBasics&gt; quesList1;
  int ctr1 = 1;
  QuestionBasics currentQ1;
  TextView txtQuestion1;
  RadioGroup grp;
  RadioButton rda1, rdb1, rdc1, rdd1;
  Button butNext1;
  Random random1 = new Random();
  ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  TextView textViewTime1;
  int number;
  ProgressBar progressBar;
  int progress = 1;
  String tableName = "", catName = "";
  TextView qstnNo;
  DatabaseHelper mDBHlpr;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_questions_basics);
  
      mDBHlpr = new DatabaseHelper(this,"blah blah blah", null,1);
      QuestionBasics myQuestion = new QuestionBasics(
              "What is note X?",
              "It's not a note",
              "A note that only xylophones can make.",
              "A note when a bowed instrument is played with two bows",
              "A synthesised x wave","It's not a note",
              "Basics");
  
  
      //mDBHlpr.addQuestionsBasics(myQuestion);
  
      List&lt;QuestionBasics&gt; mylist =  mDBHlpr.getAllQuestions1(DatabaseHelper.TABLE_QUEST1,"doesnotmatter");
      for (QuestionBasics q: mylist) {
          Log.d("QUESTIONS",q.getQUESTION1());
      }
  
      qstnNo = findViewById(R.id.qstnNo);
  
      Intent iin = getIntent();
      Bundle b = iin.getExtras();
  
      if (b != null) {
          tableName = (String) b.get("table_name");
          catName = (String) b.get("level_name");
          Log.d("Table Name", tableName);
          Log.d("Level Name", catName);
      }
      number = 0;
      //DatabaseHelper db = new DatabaseHelper(this);
      //db.addBasicsTable(tableName,catName);
      //timer
      textViewTime1 = findViewById(R.id.textViewTime);
      final CounterClass timer = new CounterClass(1800000, 1000);
      timer.start();
  //        quesList1 = db.getAllQuestions1("questBasics", catName);
  //        for (int i = 0; i &lt; 50; i++) {
  //            while (true) {
  //                int next = random1.nextInt(50);
  //                if (!list.contains(next)) {
  //                    list.add(next);
  //                    break;
  //                }
  //            }
  //        }
      //layout content
      currentQ1 = quesList1.get(0);
      txtQuestion1 = findViewById(R.id.textView1);
      rda1 = findViewById(R.id.radio0);
      rdb1 = findViewById(R.id.radio1);
      rdc1 = findViewById(R.id.radio2);
      rdd1 = findViewById(R.id.radio3);
      butNext1 = findViewById(R.id.button1);
      setQuestionView();
      grp = findViewById(R.id.radioGroup1);
      butNext1.setEnabled(false);
  
      grp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(RadioGroup radioGroup, int i) {
              if (i == R.id.radio0 || i == R.id.radio1 || i == R.id.radio2 || i == R.id.radio3)
                  butNext1.setEnabled(true);
          }
      });
  
      progressBar = findViewById(R.id.progressBar);
      progressBar.setMax(30);
      progressBar.setProgress(1);
      //checking answer
      butNext1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              progress = progress + 1;
              progressBar.setProgress(progress);
              RadioButton answer = findViewById(grp.getCheckedRadioButtonId());
              //Log.d("yourans", currentQ1.getANSWER1() + " " + answer.getText());
              if (currentQ1.getANSWER1().equals(answer.getText())) {
                  score++;
                  //Log.d("score", "Your score" + score1);
              } else {
                  wrongQuestListBasics.add(number, currentQ1.getQUESTION1());
                  selectedBasics.add(number, answer.getText().toString());
                  actualAnswerBasics.add(number, currentQ1.getANSWER1());
                  number++;
              }
              grp.clearCheck();
              butNext1.setEnabled(false);
              //finishing quiz
              if (ctr1 &lt; 31) {
                  if (ctr1 == 30) {
                      butNext1.setText("End Quiz");
                  }
                  currentQ1 = quesList1.get(list.get(ctr1));
                  setQuestionView();
              } else {
                  timer.onFinish();
                  timer.cancel();
              }
          }
      });
  
  }
  
  public void showResult() {
      Intent intent = new Intent(MainActivityQuizBasics.this, ResultActivity.class);
      Bundle b = new Bundle();
      b.putInt("scoreBasics", score);//Your score
      b.putString("section", "questBasics");//Your table name
      b.putString("category", catName);//Your category name
      intent.putStringArrayListExtra("wrongQuestions", wrongQuestListBasics);
      intent.putStringArrayListExtra("selectedAnswer", selectedBasics);
      intent.putStringArrayListExtra("actualAnswer", actualAnswerBasics);
      intent.putExtras(b); //Put your score to your next Intent
      startActivity(intent);
      finish();
  }
  
  private void setQuestionView() {
      txtQuestion1.setText(currentQ1.getQUESTION1());
      rda1.setText(currentQ1.getOPTA1());
      rdb1.setText(currentQ1.getOPTB1());
      rdc1.setText(currentQ1.getOPTC1());
      rdd1.setText(currentQ1.getOPTD1());
      if (ctr1 &lt; 10)
          qstnNo.setText("0" + ctr1 + "/30");
      else
          qstnNo.setText("" + ctr1 + "/30");
      ctr1++;
  }  
  
  }   
  
  }
  }
  </code></pre>
  
  <p><strong>EDITED - implemented suggested changes</strong>
  <strong>DBhelper</strong></p>
  
  <pre><code>package com.example.hp.demo.DbHelper;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  import com.example.hp.demo.Model.QuestionBasics;
  import com.example.hp.demo.Model.QuestionChords;
  import com.example.hp.demo.Model.QuestionScales;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class DatabaseHelper extends SQLiteOpenHelper {
  
  private static final String DATABASE_NAME = "Quiz.db";
  public static final String TABLE_SCORE = "score";
  public static final String SCORE_KEY_ID = "id";
  public static final String SCORE_KEY_SECTION = "section";
  public static final String SCORE_KEY_CAT = "category";
  public static final String SCORE_KEY_SCORE = "score";
  private static final String KEY_ID = "id";
  private static final String KEY_QUES = "question";
  private static final String KEY_ANSWER = "answer"; //correct option
  private static final String KEY_OPTA = "opta"; //option a
  private static final String KEY_OPTB = "optb"; //option b
  private static final String KEY_OPTC = "optc"; //option c
  private static final String KEY_OPTD = "optd"; //option d
  private static final String KEY_CAT = "category"; //category
  private static final String TABLE_QUEST = "questScales";
  public static final String TABLE_QUEST1 = "questBasics";
  private static final String TABLE_QUEST2 = "questChords";
  private SQLiteDatabase db;
  
  public DatabaseHelper(Context context, String name,         SQLiteDatabase.CursorFactory factory, int version) {
      super(context, DATABASE_NAME, factory, version);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      this.db = db;
  
      String basicsTableCreate = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST1 + " ( "
              + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
              + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
              + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT, " + KEY_OPTD + " TEXT, " + KEY_CAT + " TEXT)";
      db.execSQL(basicsTableCreate);
      addQuestionsBasics(db)
  
  
      String scoresTableCreate = "CREATE TABLE IF NOT EXISTS " + TABLE_SCORE + " ( "
              + SCORE_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SCORE_KEY_SECTION
              + " TEXT, " + SCORE_KEY_CAT + " TEXT, " + SCORE_KEY_SCORE + " INTEGER)";
      db.execSQL(scoresTableCreate);
  }
  
  
  private void addQuestionsBasics(SQLiteDatabase db) {
  
      QuestionBasics q101 = new QuestionBasics("What is pitch?", "The length of a sound", "The overall quality of a sound", "The highness or lowness of a sound", "The volume of a sound", "The highness or lowness of a sound", "B");
      this.addQuestionBasics(q101,db);
      QuestionBasics q102 = new QuestionBasics("What is 'timbre'?", "The ove", "format it", "compile it", "hardware it", "format it", "B");
      this.addQuestionBasics(q102,db);
      QuestionBasics q103 = new QuestionBasics("What is rhythm?", "The sound a metronome makes", "The organisation of music through time", "A beat that repeats itself", "The amount of beats in a bar", "The organisation of music through time", "B");
      this.addQuestionBasics(q103,db);
      QuestionBasics q104 = new QuestionBasics("What is harmony?", "A series of pitches creating a musical statement", "The result of multiple notes played simultaneously", "A strong match between 2 different notes", "The sound a chord makes when the notes are in the correct key", "A strong match between 2 different notes", "B");
      this.addQuestionBasics(q104,db);
      QuestionBasics q105 = new QuestionBasics("What is a 'melody'?", "A strong rhythm that keeps the tune going", "A series of pitches that form a musical statement", "A set of chords that works well together", "A short motif to transition 2 sections of a song", "A series of pitches that form a musical statement", "B");
      this.addQuestionBasics(q105,db);
      QuestionBasics q106 = new QuestionBasics("What is 'dynamics'?", "Changes in textures, such as switching instruments", "Variations in loudness and softness", "The movement of music between different keys", "The way the melody of a song integrates with the chords", "Variations in loudness and softness", "B");
      this.addQuestionBasics(q106,db);
      QuestionBasics q107 = new QuestionBasics("What two note note pairs have no sharp or flat between them?", "DE, AB", "DF, BC", "BC ,DE", "EF, BC", "EF, BC", "B");
      this.addQuestionBasics(q107,db);
      QuestionBasics q108 = new QuestionBasics("What is pitch?", "The length of a sound", "The overall quality of a sound", "The highness or lowness of a sound", "The volume of a sound", "The highness or lowness of a sound", "B");
      this.addQuestionBasics(q108,db);
      QuestionBasics q109 = new QuestionBasics("What is 'timbre'?", "The ove", "format it", "compile it", "hardware it", "format it", "B");
      this.addQuestionBasics(q109,db);
      QuestionBasics q110 = new QuestionBasics("What is rhythm?", "The sound a metronome makes", "The organisation of music through time", "A beat that repeats itself", "The amount of beats in a bar", "The organisation of music through time", "B");
      this.addQuestionBasics(q110,db);
  
  //shortened for testing
  }
  
  public void addQuestionsBasics(QuestionBasics quest) {
      addQuestionBasics(quest,this.getWritableDatabase());
  }
  
  public void addQuestionBasics(QuestionBasics quest,SQLiteDatabase db) {
      ContentValues contentValues = new ContentValues();
      contentValues.put(KEY_QUES, quest.getQUESTION1());
      contentValues.put(KEY_ANSWER, quest.getANSWER1());
      contentValues.put(KEY_OPTA, quest.getOPTA1());
      contentValues.put(KEY_OPTB, quest.getOPTB1());
      contentValues.put(KEY_OPTC, quest.getOPTC1());
      contentValues.put(KEY_OPTD, quest.getOPTD1());
      contentValues.put(KEY_CAT, quest.getCATEGORY1());
      // Inserting Row
      db.insert(TABLE_QUEST1, null, contentValues);
  }
  
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST1);
      onCreate(db);
  }
  
  public List&lt;QuestionBasics&gt; getAllQuestions1(String tname, String lname) {
      List&lt;QuestionBasics&gt; quesList1 = new ArrayList&lt;QuestionBasics&gt;();
      String selectQuery1 = "SELECT  * FROM " + tname + " WHERE " + KEY_CAT + " = '" + lname + "'";
      db = this.getReadableDatabase();
      Cursor cursor = db.rawQuery(selectQuery1, null);
      // looping through all rows and adding to list
      if (cursor.moveToFirst()) {
          do {
              QuestionBasics quest1 = new QuestionBasics();
              quest1.setID1(cursor.getInt(0));
              quest1.setQUESTION1(cursor.getString(1));
              quest1.setANSWER1(cursor.getString(2));
              quest1.setOPTA1(cursor.getString(3));
              quest1.setOPTB1(cursor.getString(4));
              quest1.setOPTC1(cursor.getString(5));
              quest1.setOPTD1(cursor.getString(6));
              quesList1.add(quest1);
          } while (cursor.moveToNext());
      }
      // return quest list
      return quesList1;
  }
  
  public List&lt;QuestionScales&gt; getAllQuestions(String tname, String lname) {
      List&lt;QuestionScales&gt; quesList1 = new ArrayList&lt;QuestionScales&gt;();
      String selectQuery1 = "SELECT  * FROM " + tname + " WHERE " + KEY_CAT + " = '" + lname + "'";
      db = this.getReadableDatabase();
      Cursor cursor = db.rawQuery(selectQuery1, null);
      // looping through all rows and adding to list
      if (cursor.moveToFirst()) {
          do {
              QuestionScales quest1 = new QuestionScales();
              quest1.setID(cursor.getInt(0));
              quest1.setQUESTION(cursor.getString(1));
              quest1.setANSWER(cursor.getString(2));
              quest1.setOPTA(cursor.getString(3));
              quest1.setOPTB(cursor.getString(4));
              quest1.setOPTC(cursor.getString(5));
              quest1.setOPTD(cursor.getString(6));
              quesList1.add(quest1);
          } while (cursor.moveToNext());
      }
      // return quest list
      return quesList1;
  }
  
  public boolean insertScoreBasics(int scoreBasics, String tname, String cname) {
      SQLiteDatabase db = this.getWritableDatabase();
  
      ContentValues contentValues = new ContentValues();
      contentValues.put(SCORE_KEY_SECTION, tname);
      contentValues.put(SCORE_KEY_CAT, cname);
      contentValues.put(SCORE_KEY_SCORE, scoreBasics);
      long resultscore = db.insert(TABLE_SCORE, null, contentValues);
      if (resultscore == -1)
          return false;
      else
          return true;
  }
  
  public int getScoreBasics() {
      Cursor c;
      SQLiteDatabase db = this.getWritableDatabase();
      String sqlSelectQuery = "SELECT MAX(" + SCORE_KEY_SCORE + ") FROM " + TABLE_SCORE + " WHERE " + SCORE_KEY_SECTION + " = '" + TABLE_QUEST1 + "' AND " + SCORE_KEY_CAT + " = " + "'I'";
      c = db.rawQuery(sqlSelectQuery, null);
      c.moveToFirst();
      int x = c.getInt(0);
      return x;
  }
  }
  </code></pre>
  
  <p><strong>xml file</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_margin="10dp"
  android:padding="10dp"
  tools:context=".MainActivityQuizBasics"&gt;
  
  &lt;LinearLayout
      android:id="@+id/linearLayout"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentBottom="true"
      android:orientation="vertical"&gt;
  
      &lt;RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"&gt;
  
          &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="top|left"
              android:background="@drawable/rect_background_pink"&gt;
  
              &lt;TextView
                  android:id="@+id/qstnNo"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:paddingLeft="8dp"
                  android:paddingRight="8dp"
                  android:singleLine="true"
                  android:text="01/30"
                  android:textColor="@color/white"
                  android:textSize="20dp"
                  android:textStyle="bold" /&gt;
  
          &lt;/RelativeLayout&gt;
  
  
          &lt;TextView
              android:id="@+id/textViewTime"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:text="05:00"
              android:textColor="@color/colorAccent"
              android:textSize="25dp"
              android:textStyle="bold" /&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;ProgressBar
          android:id="@+id/progressBar"
          style="@style/CustomProgressBar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dp"
          android:layout_marginBottom="5dp"
          android:max="30" /&gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Large Text"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textColor="@color/colorPrimaryDark" /&gt;
  
      &lt;RadioGroup
          android:id="@+id/radioGroup1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="30dp"
          android:layout_weight="0.03"&gt;
  
          &lt;RadioButton
              android:id="@+id/radio0"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:checked="false"
              android:text="RadioButton"
              android:textColor="@color/colorPrimaryDark" /&gt;
  
          &lt;RadioButton
              android:id="@+id/radio1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="RadioButton"
              android:textColor="@color/colorPrimaryDark" /&gt;
  
          &lt;RadioButton
              android:id="@+id/radio2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="RadioButton"
              android:textColor="@color/colorPrimaryDark" /&gt;
  
          &lt;RadioButton
              android:id="@+id/radio3"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="RadioButton"
              android:textColor="@color/colorPrimaryDark" /&gt;
  
  
      &lt;/RadioGroup&gt;
  &lt;/LinearLayout&gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_marginLeft="30dp"
      android:layout_marginTop="15dp"
      android:layout_marginRight="30dp"
      android:background="@color/deeppurple"
      android:fontFamily="sans-serif"
      android:text="NEXT"
      android:textColor="#fff" /&gt;
  </code></pre>
  
  <p></p>