gpt4 book ai didi

kotlin - 如何修复 "Unresolved reference. None of the following candidates is applicable because of receiver type mismatch"

转载 作者:行者123 更新时间:2023-12-01 05:49:02 26 4
gpt4 key购买 nike

Unresolved reference 。由于接收器类型不匹配,以下候选都不适用

我尝试创建倒数计时器,但出现错误

Kotlin 文件:

import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main5.*

class Main5Activity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main5)
val timer = MyCounter(10000, 1000)
start.setOnClickListener { timer.start() }
stop.setOnClickListener { timer.cancel() }
}
}

class MyCounter(millisInFuture: Long, countDownInterval: Long) :
CountDownTimer(millisInFuture, countDownInterval) {

override fun onFinish() {
println("Timer Completed.")
qq.text = "Timer Completed."

}

override fun onTick(millisUntilFinished: Long) {
qq.textSize = 50f

qq.text = (millisUntilFinished / 1000).toString() + ""
println("Timer : " + millisUntilFinished / 1000)
}
}

ACTIVITY_MAIN5:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main5Activity">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pomodoro_timer"
android:textAlignment="center"
android:textSize="40sp"
tools:ignore="MissingConstraints"/>

<Button
android:id="@+id/start"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:background="#55ff00"
android:text="@string/start_timer"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp">

</Button>

<Button
android:id="@+id/stop"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:background="#ff0000"
android:text="@string/stop_timer"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="8dp">

</Button>

<TextView
android:id="@+id/qq"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="332dp"
android:text=""
android:textAlignment="center"
android:textColor="#ff4000"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="0dp"
tools:ignore="MissingConstraints"/>


</androidx.constraintlayout.widget.ConstraintLayout>

我在 kotlin 文件中遇到 qq(在activity_main5 中描述)的问题

Unresolved reference 。由于接收器类型不匹配,以下候选都不适用:
public val Activity.qq: TextView! defined in 
kotlinx.android.synthetic.main.activity_main5 in file activity_main5.xml
public val Dialog.qq: TextView! defined in
kotlinx.android.synthetic.main.activity_main5 in file
activity_main5.xml``
public val android.app.Fragment.qq: TextView! defined in
kotlinx.android.synthetic.main.activity_main5 in file activity_main5.xml
public val androidx.fragment.app.Fragment.qq: TextView! defined in
kotlinx.android.synthetic.main.activity_main5 in file activity_main5.xml

最佳答案

您必须添加 LayoutContainer到您的计时器类,然后创建一个具有事件 View 的实例:

class MyCounter(millisInFuture: Long, countDownInterval: Long, override val containerView: View?) :
CountDownTimer(millisInFuture, countDownInterval), LayoutContainer {

override fun onFinish() {
println("Timer Completed.")
qq.text = "Timer Completed."

}

override fun onTick(millisUntilFinished: Long) {
qq.textSize = 50f

qq.text = (millisUntilFinished / 1000).toString() + ""
println("Timer : " + millisUntilFinished / 1000)
}
}

class Main5Activity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main5)
val timer = MyCounter(10000, 1000, findViewById(android.R.id.content))
start.setOnClickListener { timer.start() }
stop.setOnClickListener { timer.cancel() }
}
}

关于kotlin - 如何修复 "Unresolved reference. None of the following candidates is applicable because of receiver type mismatch",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57352209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com