我们如何在 Kotlin 上连接两个微调器?

How do we connect two spinners on Kotlin?

我想做什么

我正在尝试使用前一个堆栈溢出问题上的代码将一个微调器连接到 Kotlin 上的另一个微调器

问题

前一个程序的原始代码是用 Java 编写的,Android Studio 自动将其转换为 Kotlin。

但是,MainActivity.kt 第 17 行和第 18 行出现错误。

我应该修复什么来模拟程序?

MainActivity.kt

    internal var spinner1: Spinner
    internal var spinner2: Spinner

错误信息

Property must be initialized or be abstract -

当前代码

MainActivity.kt

package com.example.twospinners

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.AdapterView
import android.R
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import android.widget.Spinner
import android.widget.AdapterView.OnItemSelectedListener
import android.app.Activity


class MainActivity : Activity(), OnItemSelectedListener {

    internal var spinner1: Spinner
    internal var spinner2: Spinner

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        spinner1 = findViewById(R.id.spinner1) as Spinner
        spinner2 = findViewById(R.id.spinner2) as Spinner
        val adapter1 = ArrayAdapter.createFromResource(
            this,
            R.array.array1, android.R.layout.simple_spinner_item
        )
        spinner1.adapter = adapter1
        spinner1.onItemSelectedListener = this
    }

    fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        val id = item.getItemId()
        return if (id == R.id.action_settings) {
            true
        } else super.onOptionsItemSelected(item)
    }

    fun onItemSelected(
        parent: AdapterView<*>, view: View, position: Int,
        id: Long
    ) {
        if (spinner1.selectedItem == "mobile") {
            Toast.makeText(
                applicationContext, "Mobil dipilih",
                Toast.LENGTH_SHORT
            ).show()

            val adapter2 = ArrayAdapter.createFromResource(
                this,
                R.array.mobile_array, android.R.layout.simple_spinner_item
            )
            spinner2.adapter = adapter2
        } else {
            val adapter2 = ArrayAdapter.createFromResource(
                this,
                R.array.motor_array, android.R.layout.simple_spinner_item
            )
            spinner2.adapter = adapter2
        }

    }

    override fun onNothingSelected(parent: AdapterView<*>) {
        // TODO Auto-generated method stub
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.stackspinner.MainActivity" >

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

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1" />

</RelativeLayout>

strings.xml

<resources>

    <string name="app_name">Application Name</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

    <string-array name="array1">
        <item>mobile</item>
        <item>motor</item>
    </string-array>
    <string-array name="mobile_array">
        <item>Android</item>
        <item>blackberry</item>
        <item>apple</item>
    </string-array>
    <string-array name="motor_array">
        <item>Audi</item>
        <item>BMW</item>
        <item>unicorn</item>
    </string-array>
</resources>

开发环境

Android Studio 3.5.3

模拟器 Pixel2 API 28

OS 4.4 奇巧

您必须初始化这些字段或向其添加 lateinit。 所以,解决方案是:

internal lateinit var spinner1: Spinner internal lateinit var spinner2: Spinner

如果你使用 kotlin-android-extensions,你可以完全忘记 findViewById,查看这个指南 https://antonioleiva.com/kotlin-android-extensions/

与此同时,为什么不对那些微调器变量使用 lateinit? https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables

您需要在 kotlin lateinit 中声明变量或像这样初始化为 null,

方法:- 1

internal lateinit var spinner1: Spinner
internal lateinit var spinner2: Spinner

方法:- 2

internal var spinner1: Spinner? =null
internal var spinner2: Spinner? =null