[kotlin]add the button function, it can't run

Viewed 68

In MainActivity.kt, add the button function, it can't run.

How to solve?

Thank!

add the button function, it can't run.

binding.button1.setOnClickListener {
  binding.text.text = "123"
}

drive.google.com

Source code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  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=".MainActivity">

  <Button
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   app:backgroundTint="#702020" />
    
  <TextView
   android:id="@+id/text"
   android:text="text"
   android:textSize="50dp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />

  <fragment
   android:id="@+id/nav_host_fragment"
   android:name="com.android.myapplication.Fragment"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:layout="@layout/fragment_"
   />

</LinearLayout>

fragment_.xml

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".Fragment">
</FrameLayout> 

MainActivity.kt

class MainActivity : AppCompatActivity() {
   val binding: ActivityMainBinding by viewbind()
        
   override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   setContentView(R.layout.activity_main)
   binding.button1.setOnClickListener {
   binding.text.text = "123"
  }
 }
}

Fragment.kt

class Fragment : Fragment() {
 override fun onCreateView(
   inflater: LayoutInflater, container: ViewGroup?,
   savedInstanceState: Bundle?
   ): View? {
   // Inflate the layout for this fragment
   return inflater.inflate(R.layout.fragment_, container, false)
   }

error code enter image description here

2 Answers

Add below Gradle dependency

implementation "androidx.fragment:fragment-ktx:1.3.1"

Replace <fragment> with following code

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="com.android.myapplication.Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_" />

another replace in MainActivity.kt

class MainActivity : AppCompatActivity() {
    val binding: ActivityMainBinding by viewbind()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.add(R.id.nav_host_fragment, com.android.myapplication.Fragment())
        fragmentTransaction.show(com.android.myapplication.Fragment())
        fragmentTransaction.commit()

        binding.button1.setOnClickListener {
            binding.text.text = "123"
        }
Related