これはすべてkotlinにあります。
スピナーを持っていますが、動的に入力できません。しかし、strings.xmlリソースからの配列でそれを埋めることはできます。
これは、strings.xmlの配列を含まないスピナーの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"/>
ただし、リソースの配列を使用するのではなく、動的に入力する必要はありません。スピナーは、RelativeLayoutクラスでのみ作成できます。
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_dropdown_itemの代わりにsimple_spinner_itemを使用してみましたが、同じ結果になりました。問題はよくわかりません。クラスがアクティビティやフラグメントではないという事実と関係があると思いますが、確信が持てず、オンラインで何も見つかりませんでした。
私が見た:AndroidスピナーはNullPointExceptionを与える
それらのどれも助けにはなりません。
スピナーのバッキング配列に35個のエントリがあると推測します。あなたのライン
val arrayAdapter = ArrayAdapter(context, brandToAbbreviationsDivisions.keys.size, brandToAbbreviationsDivisions.keys.toList())
間違っている。2番目の引数は、配列のサイズではなく、リソースである必要があります。ArrayAdapterを参照してください。Androidは、アレイのサイズであるIDを持つリソースを探しています。そのため、の奇妙なリソースIDが0x23
表示され、クラッシュが表示されます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加