私はAndroidとKotlinの初心者ですが、私は解決できないという問題に直面しています。私はactivity_main.xml
ファイルにseekBar
とTextView
を含んでいます。 seekBar
を変更すると、変更した値をTextView
にリアルタイムで表示したいと考えています。 新しいことを学び、Kotlinを練習するには、Seekbar.OnSeekBarChangeListener
を含む新しいクラスを作成し、そのクラスはseekBar
の変更に反応します。しかし、問題は、私がを新しいseekBar
の値に設定すると、findViewById
NullPointException
という結果になります。KotlinのカスタムクラスでTextViewにアクセスする
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())
}
}
クラス:
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?) {
}
}
activity_main.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では、それは我々がkotlinx.android.synthetic.main.activity_mainインポート場合はウィジェットを参照するが、KotlinにFindViewByIdとSetContentViewの方法を使用して、簡単です。*我々はfindViewByIdを使用する必要はありません以上。私はMainActivityからコンテキストを渡そうとしましたが、それは助けにはなりません。
ありがとうございますが、問題はどのようにこの問題を解決するということではありません最も簡単な方法。私はカスタムクラスからウィジェットにアクセスする方法を練習しているだけなので、 'seekBar'リスニングクラスとそのクラスが' NullPointException'を引き起こしているのです。私は最新のAndroid Studioを使用しています。 – Kristof
私は10分を与えることを理解しています。 –