question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ChipGroup] Copy last value instead IndexOutOfBoundsException

See original GitHub issue

Description: When iterating over collection with indices (in case collection size is bigger than 1) if we add +1 index there’ll be copy chip of last value instead IndexOutOfBoundsException.

Source code:

for (i in tags.indices + 1) {
    val chip = Chip(tagsChipGroup.context).apply {
        text = tags[i]
        setChipBackgroundColorResource(R.color.colorCardBackground)
        isClickable = true
        textSize = resources.getDimension(R.dimen.tag_text_size)
     }
     tagsChipGroup.addView(chip)
}

Android API version: 29

Material Library version: 1.1.0-beta02

Device: Pixel 2 XL

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hunterstichcommented, Nov 14, 2019

Hey @papuge! This looks like a Kotlin questions that doesn’t really relate to our library.

With that said, in your original code snippet you have for (i in tags.indices + 1). What tags.indices + 1 is actually doing is creating an IntArray of the indices in your tags Collection and then appending the Int 1 to that array, leaving you with the equivalent of for (i in [0, 1, 2, 1] (given a tags array with 3 items in it). This is more obvious if you replace 1 with something like 99. tags.indices + 99 creates an IntArray of the indices in tags and appends the Int 99, leaving you with [0, 1, 2, 99]. In that case, you would get an IndexOutOfBounds exception if your tags array had less than 99 items in it.

Hopefully that helps. I’m going to go a head and close this issue.

0reactions
papugecommented, Nov 14, 2019

@hunterstich this is tag collection val tags = arguments?.getStringArrayList(ARG_TAGS)?.toList()!! and piece of OnCreateView() method

        val tags = arguments?.getStringArrayList(ARG_TAGS)?.toList()!!
        val note = arguments?.getParcelable<Note>(ARG_NOTE)
        note.run {
            headerTextView.text = this?.title ?: ""
            contentTextView.text = this?.content ?: ""

            Log.d("NoteFragment", "tags from viewModel: $tags")

            if (tags.isNotEmpty()) {
                for (tag in tags) {
                    val chip = Chip(tagsChipGroup.context).apply {
                        text = tag
                        setChipBackgroundColorResource(R.color.colorCardBackground)
                        isClickable = true
                        textSize = resources.getDimension(R.dimen.tag_text_size)
                    }
                    tagsChipGroup.addView(chip)
                }
            } else {
                view.findViewById<HorizontalScrollView>(R.id.tags_scroll).visibility = GONE
            }
        }

        editNoteButton.setOnClickListener {
            redirectToEditFragment(note!!, tags)
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Material Chips: java.lang.IndexOutOfBoundsException when ...
I have a chip group with some chips in my AddItemFragment . I am creating these chips programmatically. To be more precise here...
Read more >
ChipGroup - Android Developers
A ChipGroup is used to hold multiple Chip s. By default, the chips are reflowed across multiple lines. Set the app:singleLine attribute to ......
Read more >
WolframAlpha v1.4.19.2022041167 (Patched).apk
Allows applications to open network sockets. android.permission.ACCESS_NETWORK_STATE, Allows applications to access information about networks.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found