类型 ID 的预期资源
Expected resource of type ID
我正在使用 ANKO
构建简单的项目。当我尝试为 EditText
设置 id 时出现以下错误。
Ensures that resource id's passed to APIs are of the right type; for example, calling Resources.getColor(R.string.name)
is wrong.
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import org.jetbrains.anko.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
relativeLayout {
editText{
id = 1
}
}
}
}
你应该传递一个 id R.id.some_name
你不能在上面传递 整数。
例子:
editText {
id = R.id.userNameEditText
hintResource = R.string.create_user_hint_username
textSize = 24f
}
希望对您有所帮助:)
您应该为视图的 ID 传递一个正确的唯一资源 ID,而不仅仅是任何 int 值。
首先在 res/values/
下创建一个文件 ids.xml
并为您的 EditText
添加一个 id
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="edit_text" type="id"/>
</resources>
现在将其用作 EditText
id
:
relativeLayout {
editText{
id = R.id.edit_text
}
}
您可以使用 ID 引用 (R.id.something
) 或从 -16777215
到 -1
的负整数(并忽略警告)。
也可以使用View.generateViewId()
method available since API 17. Source code is available here 为您生成合适的负整数。您必须自己记住生成的值。
生成一个 R.id
包含在其他答案中。
I am trying to do same but getting an error
该视频是一年多前发布的,当时 lint 并未就 Kotlin 代码向您发出警告。
我正在使用 ANKO
构建简单的项目。当我尝试为 EditText
设置 id 时出现以下错误。
Ensures that resource id's passed to APIs are of the right type; for example, calling
Resources.getColor(R.string.name)
is wrong.
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import org.jetbrains.anko.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
relativeLayout {
editText{
id = 1
}
}
}
}
你应该传递一个 id R.id.some_name
你不能在上面传递 整数。
例子:
editText {
id = R.id.userNameEditText
hintResource = R.string.create_user_hint_username
textSize = 24f
}
希望对您有所帮助:)
您应该为视图的 ID 传递一个正确的唯一资源 ID,而不仅仅是任何 int 值。
首先在 res/values/
下创建一个文件 ids.xml
并为您的 EditText
添加一个 id
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="edit_text" type="id"/>
</resources>
现在将其用作 EditText
id
:
relativeLayout {
editText{
id = R.id.edit_text
}
}
您可以使用 ID 引用 (R.id.something
) 或从 -16777215
到 -1
的负整数(并忽略警告)。
也可以使用View.generateViewId()
method available since API 17. Source code is available here 为您生成合适的负整数。您必须自己记住生成的值。
生成一个 R.id
包含在其他答案中。
I am trying to do same but getting an error
该视频是一年多前发布的,当时 lint 并未就 Kotlin 代码向您发出警告。