gpt4 book ai didi

kotlin - 从类中获取可为空的变量

转载 作者:行者123 更新时间:2023-12-02 13:34:41 26 4
gpt4 key购买 nike

首先,我说我的Kotlin经验有限。我试图从一个类中获取可为空的var,但是每执行一次它都获取一个空指针。

类(class)代码:

class MultiSpinner : Spinner, OnMultiChoiceClickListener, OnCancelListener {

private var items: List<String>? = null
private var selected: BooleanArray? = null
private var selectedNew: BooleanArray? = null
private var defaultText: String? = null
private var listener: MultiSpinnerListener? = null
var category: String = "010"

constructor(context: Context) : super(context) {}

constructor(arg0: Context, arg1: AttributeSet) : super(arg0, arg1) {}

constructor(arg0: Context, arg1: AttributeSet, arg2: Int) : super(arg0, arg1, arg2) {}

override fun onClick(dialog: DialogInterface, which: Int, isChecked: Boolean) {
selectedNew!![which] = isChecked
}

override fun onCancel(dialog: DialogInterface) {
// refresh text on spinner
val spinnerBuffer = StringBuffer()
var allUnselected = true
for (i in items!!.indices) {
if (selected!![i]) {
spinnerBuffer.append(items!![i])
spinnerBuffer.append(", ")
allUnselected = false
}
selectedNew!![i] = selected!![i]
}

var spinnerText: String?
if (allUnselected) {
spinnerText = defaultText
} else {
spinnerText = spinnerBuffer.toString()

/** Remove trailing comma*/
spinnerText = spinnerText.substring(0, spinnerText.length - 2)
}
val adapter = ArrayAdapter(
context,
R.layout.simple_spinner_item,
arrayOf(spinnerText)
)
setAdapter(adapter)

category = selected!!.joinToString(limit = selected!!.size, separator = "") {it.toInt().toString()}
listener!!.onItemsSelected(selected)
}

override fun performClick(): Boolean {
val builder = AlertDialog.Builder(context)
builder.setMultiChoiceItems(
items!!.toTypedArray(), selectedNew, this
)
builder.setPositiveButton(R.string.ok
) { dialog, _ -> selected = selectedNew!!.copyOf(); dialog.cancel() }
builder.setNegativeButton(R.string.cancel
) { dialog, _ -> dialog.cancel() }
builder.setOnCancelListener(this)
builder.show()
return true
}

fun setItems(
items: List<String>, allText: String,
listener: MultiSpinnerListener
) {
this.items = items
this.defaultText = allText
this.listener = listener

// one selected by default
selected = BooleanArray(items.size) {false}
selectedNew = BooleanArray(items.size) {false}
for (i in selected!!.indices) {
selected!![i] = false
selectedNew!![i] = false
}

selected!![0] = true
selectedNew!![0] = true
category = selected!!.joinToString(limit = selected!!.size, separator = "") {it.toInt().toString()}

// all text on the spinner
val adapter = ArrayAdapter(
context,
R.layout.simple_spinner_item, arrayOf(allText)
)
setAdapter(adapter)
}

fun getSelected(): BooleanArray? {
return selected
}

interface MultiSpinnerListener {
fun onItemsSelected(selected: BooleanArray?)
}

private fun Boolean.toInt() = if (this) 1 else 0
}

初始化中
override fun onCreate(savedInstanceState: Bundle?) {
...
val spinnerCategory: MultiSpinner = findViewById(R.id.category_spinner)
val categoryList: List<String> = resources.getStringArray(R.array.category).toList()

spinnerCategory.setItems(categoryList, getString(R.string.default_category), this)
...
}


selected访问 MultiSpinner
val selected: BooleanArray? = MultiSpinner(this).getSelected()
println(selected)


println读取 null

我尝试返回 BooleanArray而不是 BooleanArray?getSelected,但这只是给了我一个空指针异常(如println阅读所期望的那样)。

我现在通过在类中使用 category并在类内部执行操作来解决该问题。但是,我想在我的主要 Activity 中获取 selected数组,并使用其数据在类之外创建所需的String。

为什么不获取 selected数据,而是获取空指针?

最佳答案

您必须使用从selected获得的实例( View 层次结构中已经存在的实例)访问findViewById,而不是自己创建一个全新的对象:

val selected: BooleanArray? = spinnerCategory.getSelected()

编辑:

如果您需要在 selected方法之外访问 onCreate,只需将 spinnerCategory存储为属性而不是局部变量即可:
private lateinit var spinnerCategory: MultiSpinner

override fun onCreate(savedInstanceState: Bundle?) {
spinnerCategory = findViewById(R.id.category_spinner)
...
}

Kotlin还为Android提供了一个不错的功能,称为 View Binding,它可以让您忽略调用 findViewById并直接使用其ID引用 View :
override fun onCreate(savedInstanceState: Bundle?) {
...
category_spinner.setItems(categoryList, getString(R.string.default_category), this)
...
}

private fun doSomethingWithSelected() {
val selected: BooleanArray? = category_spinner.getSelected()
}

但是在这种情况下 我建议您更改 View 的ID,使其与Kotlin变量命名准则一致,例如到 spinnerCategory,因为它已经被使用了。

关于kotlin - 从类中获取可为空的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com