java.lang.IndexOutOfBoundsException: Index: 0, Size: 0. Что делать?
Все мозги себе уже сломал выдает ошибку и все.
В чем суть: в классе есть функция которая выводит вопросы из одного массива и ответы из другого(массивы я передаю) массивы инициальзированы и переданы, но выдается ошибка.
Код класса:
class MainClass(
private val buttonNext: TextView,
var QuestionView: TextView,
var AnswerView1: TextView,
var AnswerView2: TextView,
var AnswerView3: TextView,
private val questions: ArrayList<String>,
private val answers: ArrayList<String>,
private val contex: Context
) {
private var score = 0
private var nowQuestion = 0
private var nowAnswer = 0
@SuppressLint("SetTextI18n")
fun main() {
for (x in questions) {
QuestionView.text = questions[nowQuestion]
for (y in 1..3) {
if (y == 1) {
AnswerView1.text = answers[nowAnswer]; nowAnswer++
}
if (y == 2) {
AnswerView2.text = answers[nowAnswer]; nowAnswer++
}
if (y == 3) {
AnswerView3.text = answers[nowAnswer]; nowAnswer++
}
}
nowQuestion++
}
}
}
Код файла где вызываю функцию:
package com.dmitry.searchgame
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.search_game_test.*
class SearchGameTest : AppCompatActivity() {
private val context: Context = this
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.search_game_test)
val questions = ArrayList<String>()
questions.add(0,"Где зарегистрирована компания Яндекс?")
questions.add(1,"Сколько запросов обрабатывается в месяц?")
val answers = ArrayList<String>()
questions.add(0,"Яндекс - это Российская компания, зарегистрированная в Нидерландах.")
questions.add(1,"Яндекс — это Американская компания, зарегистрированная там же.")
questions.add(2,"Яндекс — это компания, зарегистрированная в Москве.")
questions.add(3,"6.3 миллиардов в месяц (дата: начало 2014).")
val c = MainClass(buttonNext, questionField, answerField1, answerField2,
answerField3, questions, answers, context)
c.main()
}
}