FloatingActionButton onClickListener 不起作用
FloatingActionButton onClickListener doesn't work
所以我想用 floatingActionButton
打开新的 activity
,但它不起作用。我检查了 logcat 它甚至没有执行 onclicklistener
这是我的代码:
主要活动
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")
}
}
添加音乐活动
class AddMusicActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_music)
}
}
清单
<?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>
是的,问题是你应该在 setContentView 方法中设置绑定视图而不是布局 id,试试这个
setContentView(binding.root)
所以我想用 floatingActionButton
打开新的 activity
,但它不起作用。我检查了 logcat 它甚至没有执行 onclicklistener
这是我的代码:
主要活动
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")
}
}
添加音乐活动
class AddMusicActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_music)
}
}
清单
<?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>
是的,问题是你应该在 setContentView 方法中设置绑定视图而不是布局 id,试试这个
setContentView(binding.root)