Android Question InputList & Back Button Crash

jmoeller

Member
Licensed User
Longtime User
As you will see, I am a novice programmer. I have, what I think is a basic InputList. Everything works fine. Tap the LangButton and see the list, choose item from list, move on. However, if the user hits the OS "Back" button while the list is up, I get a crash.

LastException java.lang.ArrayIndexOutOfBoundsException:length=12; index=-3

How do I avoid this? Example code would be much appreciated.

NOTE: I am running a fairly old version of B4A v2.22

B4X:
Sub LangButton_Click
    Dim Languages As List
    Languages.Initialize
    Languages.Add("English")
    Languages.Add("Bemba")
    Languages.Add("Lozi")
    Languages.Add("Luvale")
    Languages.Add("Lunda")
    Languages.Add("Tonga")
    Languages.Add("Kaonde")
    Languages.Add("Nyanja (Lusaka)")
    Languages.Add("Nyanja (Standard)")
    LangChoice = Languages.Get(InputList(Languages, "Language Choice",0))
    LabelLang.Text = LangChoice
End Sub
 

thedesolatesoul

Expert
Licensed User
Longtime User
If you hit 'back' the InputList returns -1 (or something negative) and from your code it seems you try to fetch a negative number from your list.
Try this:
B4X:
Dim result as Int = InputList(Languages, "Language Choice",0)
If result < 0 then
  Return
Else
  LangChoice = Languages.Get(result)
End If
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…