Home > Mobile >  FloatingActionButton onClickListener doesn't work
FloatingActionButton onClickListener doesn't work

Time:05-17

so i want to open new activity with floatingActionButton, but it doesn't work. I've checked logcat and it doesn't even execute onclicklistener

here is my code:

MainActivity

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(R.layout.activity_main)


    binding.addButton.setOnClickListener {
        addButtonActivity()
        Log.e("works", "da clicked")
    }

}

private fun addButtonActivity() {
    val intent = Intent(this, AddMusicActivity::class.java)
    startActivity(intent)
    Log.e("works", "da clicked")
  }

 } 

AddMusicActivity

class AddMusicActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_add_music)
  }
 }

Manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="eu.tuto.bangercat">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BangerCat"
        tools:targetApi="31">
        <activity
            android:name=".view.activities.AddMusicActivity"
            android:exported="true" />
        <activity
            android:name=".view.activities.SplashScreenActivity"
            android:exported="false" />
        <activity
            android:name=".view.activities.MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category 
    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

CodePudding user response:

Yes, the problem is that you should to set the binding view in the method setContentView instead of the layout id, try this

setContentView(binding.root)
  • Related