androidkotlinparcelable

Why does it still say "Unresolved reference: button" even when I have button id in xml?


I have simple android app, and I want to click button to pass other activity. But it says

Unresolved reference: button

even I give id in xml. I do not know where I mistake.

Screenshot:

enter image description here

activity_main.xml:

   <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:text="Click"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

main_activity:

class MainActivity : AppCompatActivity() {

companion object {
const val USER = "user"
 }

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val user = User( "mayk",   "qqqq", PersonalInfo("mayk", "james"))


    button.setOnClickListener{
        val intent = Intent (this, Activity2::class.java)
        intent.putExtra(USER, user)
        startActivity(intent)
    }
   }
   }

Solution

  • val button = findViewById<View>(R.id.button)