前提・実現したいこと
4択のクイズアプリを作っていて、データベースに登録した問題と選択肢を画面に反映させたいと思い作っていたのですが、スタートボタンを押した後クイズ画面に遷移せずにアプリが強制終了してしまいます。
sqliteで登録したデータベースのデータをクイズ画面に表示するコードを書いてから強制終了するようになってしまったため、恐らくそこの記述がおかしいのだと思うのですが、原因がわからない状態です.
至らない点が多いかと思いますがよろしくお願いします
発生している問題・エラーメッセージ
プログラム自体にエラーは出ていないので該当しそうなlogcatを張っておきます
1666-1800/system_process E/TaskPersister: File error accessing recents directory (directory doesn't exist?). 1666-1688/system_process E/memtrack: Couldn't load memtrack module 1411-1462/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468) at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) at com.example.quiz.myapplication.SecondActivity.setQuestion(SecondActivity.java:65) at com.example.quiz.myapplication.SecondActivity.onResume(SecondActivity.java:44)
**ソースコード** SecondActivity.java public class SecondActivity extends Activity { String QuestionNo; String Seikai; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //タイトルバーを非表示にする(必ずsetContentViewの前にすること) requestWindowFeature(Window.FEATURE_NO_TITLE); // レイアウトをセットする setContentView(R.layout.activity_second); } @Override protected void onResume() { super.onResume(); // 画面↑にあるテキストを「クイズNo. + 問題No で表示 ((TextView)findViewById(R.id.textNo)).setText("クイズNo." + QuestionNo); // 問題文セット処理呼び出し setQuestion(); } // 問題文セット処理 private void setQuestion(){ // 作成したDatabaseHelperクラスに読み取り専用でアクセス DBOpenHelper dbHelper = new DBOpenHelper(this); SQLiteDatabase db = dbHelper.getReadableDatabase(); /* SELECT文 */ String sql = "SELECT * from Quizmondai"; // SQL文を実行してカーソルを取得 Cursor c = db.rawQuery(sql, null); c.moveToFirst(); // データベースから取ってきたデータを変数にセット String Kenmei = c.getString(c.getColumnIndex("Pref")); // 問題文となる都道府県 String Choice1 = c.getString(c.getColumnIndex("toi1")); // 四択の選択肢1 String Choice2 = c.getString(c.getColumnIndex("toi2")); // 四択の選択肢2 String Choice3 = c.getString(c.getColumnIndex("toi3")); // 四択の選択肢3 String Choice4 = c.getString(c.getColumnIndex("toi4")); // 四択の選択肢4 Seikai = c.getString(c.getColumnIndex("toi0")); // 答え // データベースのクローズ処理 c.close(); db.close(); ((TextView)findViewById(R.id.textQuestion)).setText(Kenmei); // 問題文となる都道府県をテキストに表示 ((Button)findViewById(R.id.button1)).setText(Choice1); // 四択の選択肢1をボタンに表示 ((Button)findViewById(R.id.button2)).setText(Choice2); // 四択の選択肢2をボタンに表示 ((Button)findViewById(R.id.button3)).setText(Choice3); // 四択の選択肢3をボタンに表示 ((Button)findViewById(R.id.button4)).setText(Choice4); // 四択の選択肢4をボタンに表示 } // 選択肢がクリックされた時の処理 public void onClick(View v){ } }
DBOpenHelper.java public class DBOpenHelper extends SQLiteOpenHelper { private static final String DB_NAME = "open.db"; //データベース名 private static final int DB_VERSION = 1; //データベースのバージョン private final Context mContext; //コンテキスト public static final String CREATE_TABLE = "create table Quizmondai "+ "(" + "_id integer primary key autoincrement" + ", Pref text" + ", toi0 text" + ", toi1 text" + ", toi2 text" + ", toi3 text" + ", toi4 text" + ", clear integer" + ")"; //コンストラクタ public DBOpenHelper(Context context){ super(context, DB_NAME, null, DB_VERSION); this.mContext = context; } //データベースが作成された時に呼ばれる //assets/sql内に定義されているsqlを実行します @Override public void onCreate(SQLiteDatabase db) { try { db.execSQL(CREATE_TABLE); execSql(db,"Quizmon"); }catch (IOException e){ e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } //引数に指定したassetsフォルダ内のsqlを実行する private void execSql(SQLiteDatabase db, String assetsDir) throws IOException { AssetManager as = mContext.getResources().getAssets(); try{ String files[] = as.list(assetsDir); for (int i = 0; i < files.length; i++){ String str = readFile(as.open(assetsDir + "/" + files[i])); for (String sql: str.split("/")){ db.execSQL(sql); } } }catch (IOException e){ e.printStackTrace(); } } //ファイルから文字列を読み込む private String readFile(InputStream is) throws IOException{ BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(is,"UTF-8")); StringBuilder sb = new StringBuilder(); String str; while ((str = br.readLine()) != null){ sb.append(str + "n"); } return sb.toString(); }finally { if (br != null)br.close(); } }
QUizmon.sql INSERT INTO Quizmondai(Pref,toi0, toi1, toi2, toi3, toi4, Clear) values ('北海道','札幌','青森','盛岡','仙台','札幌',0); INSERT INTO Quizmondai(Pref,toi0, toi1, toi2, toi3, toi4, Clear) values ('山形県','山形','山形','宇都宮','前橋','東京',0); INSERT INTO Quizmondai(Pref,toi0, toi1, toi2, toi3, toi4, Clear) values ('群馬県','前橋','横浜','前橋','京都','水戸',0); INSERT INTO Quizmondai(Pref,toi0, toi1, toi2, toi3, toi4, Clear) values ('福井県','福井','秋田','盛岡','仙台','福井',0); INSERT INTO Quizmondai(Pref,toi0, toi1, toi2, toi3, toi4, Clear) values ('石川県','金沢','前橋','京都','金沢','盛岡',0);
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/22 14:43