Причина: java .lang.IllegalStateException: Не удалось прочитать строку 0, столбец -1 из CursorWindow. Убедитесь, что курсор инициализирован правильно перед доступом к данным из него.
QuizDbHelper:
package com.example.lumeaanimalelor;
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;
import java.util.List;
public class QuizDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MyAwesomeQuiz.db";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase db;
public QuizDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
QuizContract.QuestionsTable.TABLE_NAME + " ( " +
QuizContract.QuestionsTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
QuizContract.QuestionsTable.COLUMN_QUESTION + " TEXT, " +
QuizContract.QuestionsTable.COLUMN_OPTION1 + " TEXT, " +
QuizContract.QuestionsTable.COLUMN_OPTION2 + " TEXT, " +
QuizContract.QuestionsTable.COLUMN_OPTION3 + " TEXT, " +
QuizContract.QuestionsTable.COLUMN_OPTION4 + " TEXT, " +
QuizContract.QuestionsTable.COLUMN_ANSWER_NR + " INTEGER" +
")";
db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
fillQuestionsTable();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+ QuizContract.QuestionsTable.TABLE_NAME);
onCreate(db);
}
private void fillQuestionsTable(){
Question q1 = new Question("Care dintre cele de mai jos intră în alimentația iepurelui?", "A", "B", "C", "D", 2);
addQuestion(q1);
/*Question q2 = new Question("B is correct", "A", "B", "C", "D", 2);
addQuestion(q2);
Question q3 = new Question("C is correct", "A", "B", "C", "D", 3);
addQuestion(q3);
Question q4 = new Question("A is correct", "A", "B", "C", "D", 1);
addQuestion(q4);
Question q5 = new Question("B is correct", "A", "B", "C", "D", 4);
addQuestion(q5);*/
}
private void addQuestion(Question question) {
ContentValues cv = new ContentValues();
cv.put(QuizContract.QuestionsTable.COLUMN_QUESTION, question.getQuestion());
cv.put(QuizContract.QuestionsTable.COLUMN_OPTION1, question.getOption1());
cv.put(QuizContract.QuestionsTable.COLUMN_OPTION2, question.getOption2());
cv.put(QuizContract.QuestionsTable.COLUMN_OPTION3, question.getOption3());
cv.put(QuizContract.QuestionsTable.COLUMN_OPTION4, question.getOption4());
db.insert(QuizContract.QuestionsTable.TABLE_NAME, null, cv);
}
public List<Question> getAllQuestions() {
List<Question> questionList = new ArrayList<>();
db=getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + QuizContract.QuestionsTable.TABLE_NAME, null);
if(c.moveToFirst()) {
do {
Question question = new Question();
question.setQuestion(c.getString(c.getColumnIndex(QuizContract.QuestionsTable.COLUMN_QUESTION)));
question.setOption1(c.getString(c.getColumnIndex(QuizContract.QuestionsTable.COLUMN_OPTION1)));
question.setOption2(c.getString(c.getColumnIndex(QuizContract.QuestionsTable.COLUMN_OPTION2)));
question.setOption3(c.getString(c.getColumnIndex(QuizContract.QuestionsTable.COLUMN_OPTION3)));
question.setOption4(c.getString(c.getColumnIndex(QuizContract.QuestionsTable.COLUMN_OPTION4)));
question.setAnswerNr(c.getInt(c.getColumnIndex(QuizContract.QuestionsTable.COLUMN_ANSWER_NR)));
questionList.add(question);
} while(c.moveToNext());
}
c.close();
return questionList;
}
}
Код:
package com.example.lumeaanimalelor;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Collections;
import java.util.List;
public class Iepure5 extends AppCompatActivity {
private TextView textViewQuestion;
private RadioGroup rbGroup;
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
private RadioButton rb4;
private Button buttonConfirmNext;
private ColorStateList textColorDefaultRb;
private List<Question> questionList;
private int questionCounter=0;
private int questionCountTotal;
private Question currentQuestion;
private boolean answered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iepure5);
textViewQuestion = findViewById(R.id.textView13);
rbGroup = findViewById(R.id.radio_group);
rb1 = findViewById(R.id.radioButton);
rb2 = findViewById(R.id.radioButton2);
rb3 = findViewById(R.id.radioButton3);
rb4 = findViewById(R.id.radioButton4);
buttonConfirmNext = findViewById(R.id.button13);
textColorDefaultRb = rb1.getTextColors();
showNextQuestion();
QuizDbHelper dbHelper = new QuizDbHelper(this);
questionList = dbHelper.getAllQuestions();
questionCountTotal = questionList.size();
Collections.shuffle(questionList);
buttonConfirmNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!answered) {
if(rb1.isChecked() || rb2.isChecked() || rb3.isChecked() || rb4.isChecked()) {
checkAnswer();
}
} else {
showNextQuestion();
}
}
});
}
private void showNextQuestion() {
rb1.setTextColor(textColorDefaultRb);
rb2.setTextColor(textColorDefaultRb);
rb3.setTextColor(textColorDefaultRb);
rb4.setTextColor(textColorDefaultRb);
rbGroup.clearCheck();
if(questionCounter < questionCountTotal) {
currentQuestion = questionList.get(questionCounter);
textViewQuestion.setText(currentQuestion.getQuestion());
rb1.setText(currentQuestion.getOption1());
rb2.setText(currentQuestion.getOption2());
rb3.setText(currentQuestion.getOption3());
rb4.setText(currentQuestion.getOption4());
questionCounter++;
//textViewQuestionCount.setText("Question: " + questionCounter + "/" + questionCountTotal);
answered = false;
buttonConfirmNext.setTag("Mai departe ->");
} else {
finishQuiz();
}
}
private void checkAnswer() {
answered = true;
RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
int answerNr = rbGroup.indexOfChild(rbSelected) + 1;
/*if(answerNr == currentQuestion.getAnswerNr()) {
score++;
textViewScore.setText("Score " + score);
}*/
showSolution();
}
private void showSolution() {
rb1.setTextColor(Color.RED);
rb2.setTextColor(Color.RED);
rb3.setTextColor(Color.RED);
switch (currentQuestion.getAnswerNr()) {
case 1:
rb1.setTextColor(Color.GREEN);
break;
case 2:
rb2.setTextColor(Color.GREEN);
break;
case 3:
rb3.setTextColor(Color.GREEN);
break;
}
if (questionCounter < questionCountTotal) {
buttonConfirmNext.setText("Next");
} else {
buttonConfirmNext.setText("Finish");
}
}
private void finishQuiz() { finish(); }
}
QuizContract:
package com.example.lumeaanimalelor;
import android.provider.BaseColumns;
public final class QuizContract {
private QuizContract() {}
public static class QuestionsTable implements BaseColumns {
public static final String TABLE_NAME = "quiz_questions";
public static final String COLUMN_QUESTION = "question";
public static final String COLUMN_OPTION1 = "option1";
public static final String COLUMN_OPTION2 = "option2";
public static final String COLUMN_OPTION3 = "option3";
public static final String COLUMN_OPTION4 = "option4";
public static final String COLUMN_ANSWER_NR = "answer_nr";
}
}
Вопрос:
package com.example.lumeaanimalelor;
public class Question {
private String question;
private String option1;
private String option2;
private String option3;
private String option4;
private int answerNr;
public Question() {
}
public Question(String question, String option1, String option2, String option3, String optiom4, int answerNr) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.option4 = option4;
this.answerNr = answerNr;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOption1() {
return option1;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption2() {
return option2;
}
public void setOption2(String option2) {
this.option2 = option2;
}
public String getOption3() {
return option3;
}
public void setOption3(String option3) {
this.option3 = option3;
}
public String getOption4() {
return option4;
}
public void setOption4(String option4) {
this.option4 = option4;
}
public int getAnswerNr() {
return answerNr;
}
public void setAnswerNr(int answerNr) {
this.answerNr = answerNr;
}
}
Ошибка:
D/ViewRootImpl@61dd30f[Iepure4]: ViewPostIme pointer 0
D/ViewRootImpl@61dd30f[Iepure4]: ViewPostIme pointer 1
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@981f4b8
I/DecorView: createDecorCaptionView >> DecorView@1d0ab82[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 5 rows, 6 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lumeaanimalelor, PID: 1846
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lumeaanimalelor/com.example.lumeaanimalelor.Iepure5}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:465)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.example.lumeaanimalelor.QuizDbHelper.getAllQuestions(QuizDbHelper.java:83)
at com.example.lumeaanimalelor.Iepure5.onCreate(Iepure5.java:52)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I/Process: Sending signal. PID: 1846 SIG: 9
Process 1846 terminated.