我是 Android 和 Kotlin 的新手,我遇到了一个我无法解决的问题。我有一个activity_main.xml
包含 aseekBar
和 a的文件TextView
。当我更改时,seekBar
我想TextView
实时显示更改后的值。为了学习新事物和练习 Kotlin,我创建了一个包含 a 的新类,Seekbar.OnSeekBarChangeListener
并且该类对seekBar
更改做出反应。但问题是当我将 设置TextView
为新seekBar
值时,它会导致findViewById
NullPointException
.
FATAL EXCEPTION: main
Process: kotlindemo1.kristof.kotlin_1demo, PID: 24410
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2090)
at kotlindemo1.kristof.kotlin_1demo.seekB._$_findCachedViewById(seekB.kt:0)
at kotlindemo1.kristof.kotlin_1demo.seekB.onProgressChanged(seekB.kt:15)
at android.widget.SeekBar.onProgressRefresh(SeekBar.java:93)
这是 MainActivity 类:
package kotlindemo1.kristof.kotlin_1demo
import android.app.Activity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
open class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sB1.setOnSeekBarChangeListener(seekB())
}
}
处理 SeekBar 更改的类:
package kotlindemo1.kristof.kotlin_1demo
import android.app.Activity
import android.os.Bundle
import android.widget.SeekBar
import kotlinx.android.synthetic.main.activity_main.*
class seekB : SeekBar.OnSeekBarChangeListener, Activity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if(fromUser) tW1.text = getString(R.string.max_gen_number, progress) //java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
}
活动主.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cL"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kotlindemo1.kristof.kotlin_1demo.MainActivity"
>
<SeekBar
android:id="@+id/sB1"
android:progress="100"
android:max="200"
android:layout_width="182dp"
android:layout_height="21dp"
android:layout_marginTop="60dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1"
/>
<TextView
android:id="@+id/tW1"
android:layout_width="139dp"
android:layout_height="wrap_content"
android:text=""
android:textAlignment="center"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/sB1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.502"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1" />
</android.support.constraint.ConstraintLayout>
在 Java 中,使用 FindViewById 和 SetContentView 方法来引用小部件很容易,但在 Kotlin 中,如果我们导入 kotlinx.android.synthetic.main.activity_main.*,则不需要使用 findViewById。我试图从 MainActivity 传递上下文,但这没有帮助。