質問編集履歴

3

追加

2017/05/07 09:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -237,3 +237,39 @@
237
237
  }
238
238
 
239
239
  ```
240
+
241
+ ```
242
+
243
+ public final class QuizContract {
244
+
245
+ public QuizContract() {
246
+
247
+ }
248
+
249
+
250
+
251
+ public static abstract class Quiz implements BaseColumns {
252
+
253
+ public static final String TABLE_NAME = "quiz";
254
+
255
+ public static final String _ID = "_id";
256
+
257
+ public static final String COL_QUIZ = "quiz";
258
+
259
+ public static final String COL_CORRECT = "correct";
260
+
261
+ public static final String COL_ANSWER1 = "answer1";
262
+
263
+ public static final String COL_ANSWER2 = "answer2";
264
+
265
+ public static final String COL_ANSWER3 = "answer3";
266
+
267
+ public static final String COL_ANSWER4 = "answer4";
268
+
269
+ }
270
+
271
+ }
272
+
273
+ コード
274
+
275
+ ```

2

修正

2017/05/07 09:43

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,14 @@
4
4
 
5
5
  ほかのところが原因になっている可能性がある場合は言ってください。
6
6
 
7
+ クイズはsqlで管理しているのですが最初quiz3までしか書いていなかったときは正常に動いていたのですが、113まで追加したところエラーが出てしまいましたそれが何かの原因になっていると思うのですが、、、
8
+
9
+
10
+
11
+ ご教授お願いします。
12
+
13
+
14
+
7
15
  Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
8
16
 
9
17
  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)

1

修正

2017/05/07 09:35

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,38 +1,10 @@
1
1
  タイトルのようなエラーコードが出てしまって困っています。
2
2
 
3
- 全体文字数が足りないのでコメントの方捕捉させていただきます。
4
-
5
-
6
-
7
- Process: jp.gr.java_conf.flatone.sirequiz, PID: 7140
8
-
9
- java.lang.RuntimeException: Unable to start activity ComponentInfo{sample.quiz/sample.quiz.Quiz}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10
-
11
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2248)
12
-
13
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2298)
14
-
15
- at android.app.ActivityThread.access$800(ActivityThread.java:144)
16
-
17
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
18
-
19
- at android.os.Handler.dispatchMessage(Handler.java:102)
20
-
21
- at android.os.Looper.loop(Looper.java:212)
22
-
23
- at android.app.ActivityThread.main(ActivityThread.java:5151)
24
-
25
- at java.lang.reflect.Method.invokeNative(Native Method)
26
-
27
- at java.lang.reflect.Method.invoke(Method.java:515)
28
-
29
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
30
-
31
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
32
-
33
- at dalvik.system.NativeStart.main(Native Method)
34
-
35
- Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
3
+ 文字数が足りないので自分エラーと関係しそうなところけ抜出してあります。
4
+
5
+ ほかのところが原因になっている可能性がある場合は言ってください。
6
+
7
+ Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
36
8
 
37
9
  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
38
10
 
@@ -73,3 +45,187 @@
73
45
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
74
46
 
75
47
  at dalvik.system.NativeStart.main(Native Method) 
48
+
49
+
50
+
51
+
52
+
53
+ ```
54
+
55
+ public class Quiz extends AppCompatActivity {
56
+
57
+ int score = 0;
58
+
59
+ int mode = 0;
60
+
61
+ int count = 0;
62
+
63
+ int QuizNo;
64
+
65
+ String quiz;
66
+
67
+ String correct;
68
+
69
+ String answer1;
70
+
71
+ String answer2;
72
+
73
+ String answer3;
74
+
75
+ String answer4;
76
+
77
+
78
+
79
+ @Override
80
+
81
+ protected void onCreate(Bundle savedInstanceState) {
82
+
83
+ super.onCreate(savedInstanceState);
84
+
85
+ setContentView(R.layout.activity_quiz);
86
+
87
+
88
+
89
+ Intent intent = getIntent();
90
+
91
+ mode = intent.getIntExtra(MainActivity.EXTRA_MODE,0);
92
+
93
+ if (mode == 0){
94
+
95
+ mode = intent.getIntExtra(Result.EXTRA_MODE,0);
96
+
97
+ }
98
+
99
+ random();
100
+
101
+ }
102
+
103
+
104
+
105
+ public void random(){
106
+
107
+ TextView score_text = (TextView)findViewById(R.id.score);
108
+
109
+ score_text.setText(String.valueOf(score));
110
+
111
+ Random random = new Random();
112
+
113
+ QuizNo = random.nextInt(113);
114
+
115
+
116
+
117
+ if (QuizNo == 0){
118
+
119
+ random();
120
+
121
+ }else{
122
+
123
+ set();
124
+
125
+ }
126
+
127
+
128
+
129
+ private void set(){
130
+
131
+ QuizOpenHelper quizOpenHelper = new QuizOpenHelper(this);
132
+
133
+ SQLiteDatabase sqLiteDatabase = quizOpenHelper.getReadableDatabase();
134
+
135
+ String sql = "select quiz,correct,answer1,answer2,answer3,answer4 from quiz where _id =" + QuizNo;
136
+
137
+ Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
138
+
139
+ cursor.moveToFirst();
140
+
141
+ quiz = cursor.getString(cursor.getColumnIndex("quiz"));
142
+
143
+ correct = cursor.getString(cursor.getColumnIndex("correct")); }
144
+
145
+
146
+
147
+ ```
148
+
149
+ ```
150
+
151
+ public class QuizOpenHelper extends SQLiteOpenHelper{
152
+
153
+
154
+
155
+ public static final String DB_NAME = "sirequiz.db";
156
+
157
+ public static final int DB_VERSION = 1;
158
+
159
+ public static final String CREATE_TABLE =
160
+
161
+ "create table " +QuizContract.Quiz.TABLE_NAME + "("+
162
+
163
+ QuizContract.Quiz._ID + " integer primary key autoincrement,"+
164
+
165
+ QuizContract.Quiz.COL_QUIZ + " text," +
166
+
167
+ QuizContract.Quiz.COL_CORRECT +" text," +
168
+
169
+ QuizContract.Quiz.COL_ANSWER1 +" text," +
170
+
171
+ QuizContract.Quiz.COL_ANSWER2 +" text," +
172
+
173
+ QuizContract.Quiz.COL_ANSWER3 +" text," +
174
+
175
+ QuizContract.Quiz.COL_ANSWER4 +" text)";
176
+
177
+
178
+
179
+ public static final String INIT_TABLE =
180
+
181
+ "insert into " + QuizContract.Quiz.TABLE_NAME + " (quiz,correct,answer1,answer2,answer3,answer4) values " +
182
+
183
+ "('quiz1','correct1','answer1-1','answer2-1','answer3-1','answer4-1')," +
184
+
185
+ "('quiz2','correct2','answer1-2','answer2-2','answer3-2','answer4-2')," +
186
+
187
+ .
188
+
189
+ .
190
+
191
+ .
192
+
193
+ .
194
+
195
+ "('quiz113','correct113','answer1-113','answer2-113','answer3-113','answer4-113')";
196
+
197
+
198
+
199
+ public static final String DROP_TABLE =
200
+
201
+ "drop table if exists " + QuizContract.Quiz.TABLE_NAME;
202
+
203
+ public QuizOpenHelper(Context context){
204
+
205
+ super(context,DB_NAME,null,DB_VERSION);
206
+
207
+ }
208
+
209
+ @Override
210
+
211
+ public void onCreate(SQLiteDatabase db) {
212
+
213
+ db.execSQL(CREATE_TABLE);
214
+
215
+ db.execSQL(INIT_TABLE);
216
+
217
+ }
218
+
219
+ @Override
220
+
221
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
222
+
223
+ db.execSQL(DROP_TABLE);
224
+
225
+ onCreate(db);
226
+
227
+ }
228
+
229
+ }
230
+
231
+ ```