There are two activities in the app, in the second activity I have a textView and a button. Even though I have initialized the variables by findViewById but in both the cases it is showing the message attempt to invoke on a null pointer object. I have searched and read many answer which says that may be there is any other view with same id but in my case it is the only view with this id.
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// val resultName = intent.getStringExtra("name")
val result = findViewById<TextView>(R.id.textView3)
result.text = getString(R.string.resDisplay)
val replay = findViewById<Button>(R.id.button3)
replay?.setOnClickListener {
b1.text = ""
b2.text = ""
b3.text = ""
b4.text = ""
b5.text = ""
b6.text = ""
b7.text = ""
b8.text = ""
b9.text = ""
b1.isEnabled = true
b2.isEnabled = true
b3.isEnabled = true
b4.isEnabled = true
b5.isEnabled = true
b6.isEnabled = true
b7.isEnabled = true
b8.isEnabled = true
b9.isEnabled = true
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
and this is my xml file of second layout
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/player_2_x"
android:layout_marginTop="200dp"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/play_again"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />