0

This is all in kotlin.

I have a spinner but I can't fill it dynamically. But I can fill it with an array from strings.xml resources.

Here's the xml for the spinner without the array from strings.xml:

<Spinner
        android:id="@+id/brand_name_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

If I add the array from strings.xml it's the code below, and this works as long as I don't try filling the spinner dynamically:

<Spinner
        android:id="@+id/brand_name_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/brand_names"/>

But I wan't to fill it dynamically, not use an array from resources. The Spinner can only be made in a RelativeLayout class:

class BrandSelectionStartupPage : RelativeLayout {

constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

...

Here's where I make the spinner:

init {
    LayoutInflater.from(context).inflate(R.layout.brand_selection_startup_page, this, true)
    val brandNames = findViewById<Spinner>(R.id.brand_name_input)
    val arrayAdapter =  ArrayAdapter(context, brandToAbbreviationsDivisions.keys.size, brandToAbbreviationsDivisions.keys.toList())
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    brandNames.adapter = arrayAdapter

Log of crash:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.pos, PID: 12859
android.content.res.Resources$NotFoundException: Resource ID #0x23
    at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2148)
    at android.content.res.Resources.getLayout(Resources.java:1157)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:416)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:205)
    at android.widget.Spinner.onMeasure(Spinner.java:672)
    at androidx.appcompat.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:428)
    at android.view.View.measure(View.java:23279)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:23279)

I tried using simple_spinner_item instead of simple_spinner_dropdown_item but with the same result. I'm not sure with issue is. I think it has to do with the fact that the class isn't an activity or fragment, but I'm not sure and couldn't find anything online.

I've looked at: Android spinner give a NullPointException

Add a Spinner to a RelativeLayout dynamically

Android: Create spinner programmatically from array

None of them help.

gunnersFc
  • 15
  • 2

1 Answers1

1

I am going to guess that you have 35 entries in your backing array for the spinner. Your line

val arrayAdapter =  ArrayAdapter(context, brandToAbbreviationsDivisions.keys.size, brandToAbbreviationsDivisions.keys.toList())

is incorrect. The second argument should be a resource and not the size of the array. See ArrayAdapter. Android is looking for a resource the has an id that is the size of your array. That is why we see the odd resource id of 0x23 and why you see the crash.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131