Android:Spinner 在 relativeLayout class 中给出 android.content.res.Resources$NotFoundException 错误

Android: Spinner gives android.content.res.Resources$NotFoundException error in a relativeLayout class

这一切都在 kotlin 中。

我有一个微调器,但我无法动态填充它。但我可以用 strings.xml 资源中的数组填充它。

这里是微调器的 xml,没有来自 strings.xml 的数组:

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

如果我从 strings.xml 添加数组,它是下面的代码,只要我不尝试动态填充微调器,它就可以工作:

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

但我不想动态填充它,也不想使用资源数组。 Spinner 只能在 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)

...

这是我制作微调器的地方:

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

崩溃日志:

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)

我尝试使用 simple_spinner_item 而不是 simple_spinner_dropdown_item,但结果相同。我不确定问题是什么。我认为这与 class 不是 activity 或片段这一事实有关,但我不确定,也无法在网上找到任何东西。

我看过:Android spinner give a NullPointException

Add a Spinner to a RelativeLayout dynamically

Android: Create spinner programmatically from array

None 个帮助。

我猜你的微调器支持数组中有 35 个条目。你的线路

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

不正确。第二个参数应该是资源而不是数组的大小。参见 ArrayAdapter。 Android 正在寻找一个 ID 为您的数组大小的资源。这就是为什么我们看到奇怪的资源 ID 0x23 以及为什么你看到崩溃。