Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

android - Illegal state exception OnClick Method

I am new to Kotlin and trying to make the application like taboo. I have 6 text views, one of them is the main word and the others are the forbidden words. When I click the button words are replacing with other words. However, after I click the button several times it gives me an Illegal State Exception.

var tabooList = ArrayList<WordModel>()
lateinit var wordMain : TextView
lateinit var word1 : TextView
lateinit var word2 : TextView
lateinit var word3 : TextView
lateinit var word4 : TextView
lateinit var word5 : TextView

Here are my definitions.

 wordMain = findViewById(R.id.kelimeMain)
    word1 = findViewById(R.id.kelime1)
    word2 = findViewById(R.id.kelime2)
    word3 = findViewById(R.id.kelime3)
    word4 = findViewById(R.id.kelime4)
    word5 = findViewById(R.id.kelime5)


    tabooList.add(WordModel("F??L","??","OLU?","HAREKET","EYLEM","S?ZCüK"))
    tabooList.add(WordModel("UYAK", "???R", "D?ZE", "BENZERL?K", "KAF?YE", "SES"))

This is my OnCreate Method.

fun randomWord(view: View) {
    var random = (0..tabooList.size).random()

wordMain.text = tabooList[random].anaKelime
    word1.text = tabooList[random].kelime1
    word2.text = tabooList[random].kelime2
    word3.text = tabooList[random].kelime3
    word4.text = tabooList[random].kelime4
    word5.text = tabooList[random].kelime5
}

And, this one is my button's onClick method. It gives me an error on this line firstly wordMain.text = tabooList[random].anaKelime .

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.suatkkrer.taboo_android, PID: 21946
java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
    at android.view.View.performClick(View.java:7350)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
    at android.view.View.performClickInternal(View.java:7327)
    at android.view.View.access$3600(View.java:807)
    at android.view.View$PerformClick.run(View.java:28166)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7464)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
    at android.view.View.performClick(View.java:7350)?
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)?
    at android.view.View.performClickInternal(View.java:7327)?
    at android.view.View.access$3600(View.java:807)?
    at android.view.View$PerformClick.run(View.java:28166)?
    at android.os.Handler.handleCallback(Handler.java:907)?
    at android.os.Handler.dispatchMessage(Handler.java:99)?
    at android.os.Looper.loop(Looper.java:216)?
    at android.app.ActivityThread.main(ActivityThread.java:7464)?
    at java.lang.reflect.Method.invoke(Native Method)?
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)?
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)?
 Caused by: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.suatkkrer.taboo_android.Activities.TabooActivity.randomWord(TabooActivity.kt:333)

This is the error.

question from:https://stackoverflow.com/questions/65829845/illegal-state-exception-onclick-method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Size returns the total amount of items in your list, so if you have 2 items, then the size will be 2, but arrays are zero based, so you have to add a -1 to your statement:

 var random = (0..tabooList.size-1).random()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...