Android Studio Kotlin 应用 "keeps stopping" 当我 运行 它
Android Studio Kotlin app "keeps stopping" when I run it
我只是想设置一个配置为下拉菜单的微调器,但我什至无法测试我的应用程序以查看它是否有效,因为每次我 运行 它都会立即崩溃。我知道我的问题与 MainActivity.kt 文件中第 21 行的空对象引用有关。这是问题代码:
val spinner: Spinner = findViewById<Spinner>(R.id.locations)
微调器的 id 是 位置,所以我不确定为什么它会作为空值返回。
这也是该文件的完整代码:
import android.app.Activity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Spinner
import android.widget.ArrayAdapter
import android.widget.AdapterView
private var userLocation: Any = ""
private var userDestination: Any = ""
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
val spinner: Spinner = findViewById<Spinner>(R.id.locations)
val locationsAdapter: ArrayAdapter<CharSequence> = ArrayAdapter.createFromResource(
this,
R.array.rooms,
android.R.layout.simple_spinner_item
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
}
}
class SpinnerActivity : Activity(), AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View?, pos: Int, id:Long) {
userLocation = parent.getItemAtPosition(pos)
val spinner: Spinner = findViewById(R.id.locations)
spinner.onItemSelectedListener = this
}
override fun onNothingSelected(parent: AdapterView<*>) = Unit
}
为 MainActivity 声明全局微调器:
private lateinit var spinner: Spinner
在 onCreate 方法中初始化微调器并设置其适配器,我建议您按如下方式设置数组适配器:
spinner = findViewById<Spinner>(R.id.locations)
spinner.adapter = ArrayAdapter(this, R.layout.drop_down_generic_item, resources.getStringArray(R.array.rooms))
我不太明白你在问题中展示的SpinnerActivity,抱歉。但是在 MainActivity 中你可以设置 spinner.onItemSelectedLister() 如下:
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, p3: Long) {
val selectedRoom = adapterView?.getItemAtPosition(position)
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
我不想让您感到困惑,但请看一下带有 AutoCompleteTextView 的 TextInputLayout,以作为提示。 :D
我只是想设置一个配置为下拉菜单的微调器,但我什至无法测试我的应用程序以查看它是否有效,因为每次我 运行 它都会立即崩溃。我知道我的问题与 MainActivity.kt 文件中第 21 行的空对象引用有关。这是问题代码:
val spinner: Spinner = findViewById<Spinner>(R.id.locations)
微调器的 id 是 位置,所以我不确定为什么它会作为空值返回。
这也是该文件的完整代码:
import android.app.Activity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Spinner
import android.widget.ArrayAdapter
import android.widget.AdapterView
private var userLocation: Any = ""
private var userDestination: Any = ""
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
val spinner: Spinner = findViewById<Spinner>(R.id.locations)
val locationsAdapter: ArrayAdapter<CharSequence> = ArrayAdapter.createFromResource(
this,
R.array.rooms,
android.R.layout.simple_spinner_item
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
}
}
class SpinnerActivity : Activity(), AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View?, pos: Int, id:Long) {
userLocation = parent.getItemAtPosition(pos)
val spinner: Spinner = findViewById(R.id.locations)
spinner.onItemSelectedListener = this
}
override fun onNothingSelected(parent: AdapterView<*>) = Unit
}
为 MainActivity 声明全局微调器:
private lateinit var spinner: Spinner
在 onCreate 方法中初始化微调器并设置其适配器,我建议您按如下方式设置数组适配器:
spinner = findViewById<Spinner>(R.id.locations)
spinner.adapter = ArrayAdapter(this, R.layout.drop_down_generic_item, resources.getStringArray(R.array.rooms))
我不太明白你在问题中展示的SpinnerActivity,抱歉。但是在 MainActivity 中你可以设置 spinner.onItemSelectedLister() 如下:
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, p3: Long) {
val selectedRoom = adapterView?.getItemAtPosition(position)
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
我不想让您感到困惑,但请看一下带有 AutoCompleteTextView 的 TextInputLayout,以作为提示。 :D