无法将点击侦听器设置为 Kotlin 中的元素
Can't set on click listener to an element in Kotlin
我遇到了这个问题,每当我尝试与 Kotlin 中的元素进行交互时,我都无法像以前那样 select 它。例如,如果我的 XML 中有一个 ID 为 button
的 button
,我会从 Kotlin 中执行此操作:
button.setOnClickListener {
...
}
但是现在这根本行不通,我不知道为什么...
这是我的 MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Retrofit
import retrofit2.awaitResponse
const val BASE_URL = "https://cat-fact.herokuapp.com/"
class MainActivity : AppCompatActivity() {
private var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getCurrentData()
}
private fun getCurrentData() {
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.build()
.create(ApiRequests::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.getFacts().awaitResponse()
if (response.isSuccessful) {
val data = response.body()!!
Log.d(TAG, data.text)
withContext(Dispatchers.Main) {
}
}
}
}
}
和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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center|center_horizontal"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="0.4"
android:foregroundGravity="bottom"
android:gravity="bottom"
android:orientation="vertical">
<Button
android:id="@+id/gen_new_fact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="150dp"
android:text="Generate Another Fact"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
我是 运行 Android Studio 4 的最新稳定版 Windows 10
您需要在代码中引用您的按钮。一个示例是使用 findViewById
来获取对 Kotlin 代码中 Button 的引用。它可能看起来像这样:
val button = findViewById<Button>(R.id.gen_new_fact);
button.setOnClickListener {
// Your code here.
}
我遇到了这个问题,每当我尝试与 Kotlin 中的元素进行交互时,我都无法像以前那样 select 它。例如,如果我的 XML 中有一个 ID 为 button
的 button
,我会从 Kotlin 中执行此操作:
button.setOnClickListener {
...
}
但是现在这根本行不通,我不知道为什么...
这是我的 MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Retrofit
import retrofit2.awaitResponse
const val BASE_URL = "https://cat-fact.herokuapp.com/"
class MainActivity : AppCompatActivity() {
private var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getCurrentData()
}
private fun getCurrentData() {
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.build()
.create(ApiRequests::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.getFacts().awaitResponse()
if (response.isSuccessful) {
val data = response.body()!!
Log.d(TAG, data.text)
withContext(Dispatchers.Main) {
}
}
}
}
}
和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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center|center_horizontal"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="0.4"
android:foregroundGravity="bottom"
android:gravity="bottom"
android:orientation="vertical">
<Button
android:id="@+id/gen_new_fact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="150dp"
android:text="Generate Another Fact"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
我是 运行 Android Studio 4 的最新稳定版 Windows 10
您需要在代码中引用您的按钮。一个示例是使用 findViewById
来获取对 Kotlin 代码中 Button 的引用。它可能看起来像这样:
val button = findViewById<Button>(R.id.gen_new_fact);
button.setOnClickListener {
// Your code here.
}