gpt4 book ai didi

java - 无法将 Java 代码转换为 Kotlin 以实现 OnChangeListener

转载 作者:行者123 更新时间:2023-11-29 18:43:14 24 4
gpt4 key购买 nike

我有这段代码来为 Range Bar 库 实现监听器:

rangebar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
@Override
public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex,
int rightPinIndex, String leftPinValue, String rightPinValue) {
}

});

现在我尝试通过以下方式将其导入并更改为 kotlin:

rangebar.setOnRangeBarChangeListener(object: RangeBar.OnRangeBarChangeListener {
override fun onRangeChangeListener(rangeBar:RangeBar, leftPinIndex:Int,
rightPinIndex:Int, leftPinValue:String, rightPinValue:String) {
textView.text = "$leftPinIndex"
}
})

rangebar.setOnRangeBarChangeListener { rangeBar, leftPinIndex, 
rightPinIndex, leftPinValue, rightPinValue ->
textView.text = "$leftPinIndex" }

在这两种情况下,应用都会崩溃并出现以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.appyvet.materialrangebar.RangeBar.setOnRangeBarChangeListener> (com.appyvet.materialrangebar.RangeBar$OnRangeBarChangeListener)' on a null object reference

我做错了什么?

最佳答案

你的rangebarnull

为避免崩溃,请像这样使用:

rangebar?.setOnRangeBarChangeListener(object: RangeBar.OnRangeBarChangeListener {
override fun onRangeChangeListener(rangeBar:RangeBar, leftPinIndex:Int, rightPinIndex:Int, leftPinValue:String, rightPinValue:String) {
textView.text = "$leftPinIndex"
}
})

rangebar?.setOnRangeBarChangeListener { rangeBar, leftPinIndex, rightPinIndex, leftPinValue, rightPinValue -> 
textView.text = "$leftPinIndex"
}

关于java - 无法将 Java 代码转换为 Kotlin 以实现 OnChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52540654/

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