[英]Error "unresolved reference" in Kotlin Android
I have code:我有代码:
package com.example.admin.maytinh
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.admin.maytinh.R.id.button
import com.example.admin.maytinh.R.id.editText
import com.example.admin.maytinh.R.id.editText2
import com.example.admin.maytinh.R.id.editText3
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener(View.OnClickListener{xuly()})
}
public fun xuly(){
val a:Int = editText.text.ToString().ToInt()
val b:Int = editText2.text.ToString().ToInt()
val c:Int = a + b
editText3.text = c.ToString()
}
}
When i run it, i receive errors:当我运行它时,我收到错误:
Anyone can explain for me why this is so and fix it Thank you任何人都可以为我解释为什么会这样并解决它谢谢
You importing ids, not views. 您导入ID,而不是视图。 Instead of:
代替:
import com.example.admin.maytinh.R.id.button
import com.example.admin.maytinh.R.id.editText
import com.example.admin.maytinh.R.id.editText2
import com.example.admin.maytinh.R.id.editText3
use this: 用这个:
import kotlinx.android.synthetic.main.activity_main.*
and add plugin in app gradle file: 并在app gradle文件中添加插件:
apply plugin: 'kotlin-android-extensions'
You got unresolved
error because you have not imported view. 您有
unresolved
错误,因为您没有导入视图。 Instead of view you have imported ids. 而不是查看您已导入的ID。
There import To import single view 有导入要导入单个视图
import kotlinx.android.synthetic.main.<layout_name>.<view_name>;
or 要么
To import all widget properties for a specific layout 导入特定布局的所有窗口小部件属性
import kotlinx.android.synthetic.main.<layout>.*
Also you need is to enable the Android Extensions Gradle plugin in your module's build.gradle file: 您还需要在模块的build.gradle文件中启用Android Extensions Gradle插件:
apply plugin: 'kotlin-android-extensions'
It looks like you're trying to use Kotlin Android Extensions . 看起来你正在尝试使用Kotlin Android Extensions 。 To do so import:
要这样做导入:
import kotlinx.android.synthetic.main.activity_main.*
instead of 代替
import com.example.admin.maytinh.R.id.button
import com.example.admin.maytinh.R.id.editText
import com.example.admin.maytinh.R.id.editText2
import com.example.admin.maytinh.R.id.editText3
Spend few minutes on reading how KAE work - it will help you a lot. 花几分钟时间阅读KAE如何工作 - 它会对你有很大的帮助。
Moreover, there are no such methods as ToString()
and ToInt()
. 而且,没有
ToString()
和ToInt()
这样的方法。 What you're looking for is this: toString()
and toInt()
. 您正在寻找的是:
toString()
和toInt()
。
I faced the same problem even after using migrating to viewBinding.即使在使用迁移到 viewBinding 之后,我也遇到了同样的问题。 It turns out that the problem was that I mistakenly gave my webview the same XML id then the layout it was in. I spent a weekend trying to solve it.
事实证明,问题是我错误地给了我的 webview 相同的 XML id 然后是它所在的布局。我花了一个周末试图解决它。 I guess sharing that will be useful for someone else.
我想分享对其他人有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.