`Anko` 给出异常 AnkoException: Id is not set
`Anko` gives exception AnkoException: Id is not set
我正在尝试学习 Anko
的布局设计。我已经使用 Anko
设置了布局,如下所示
relativeLayout {
val counterTextView = textView {
text = "0"
textSize = 24f
}
button {
onClick {
count++
counterTextView.text = count.toString()
}
}.lparams {
below(counterTextView)
}
}
我只需要一个带有 TextView 和 TextView 下方按钮的相对布局。
以上代码报错如下
Caused by: org.jetbrains.anko.AnkoException: Id is not set for android.widget.TextView{d8dedcd V.ED..... ......ID 0,0-0,0}
at org.jetbrains.anko.RelativeLayoutLayoutParamsHelpersKt.below(RelativeLayoutLayoutParamsHelpers.kt:60)
at com.example.app.MainActivity.onCreate(MainActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
有人知道怎么解决吗?
我想如果你想使用 relativeLayout
参数,你必须为视图创建 id,所以试试这个
relativeLayout {
val counterTextView = textView {
id = R.id.counterTextView // add this line
text = "0"
textSize = 24f
}
button {
onClick {
count++
counterTextView.text = count.toString()
}
}.lparams {
below(counterTextView)
}
}
并且您必须在值文件夹
中创建 ids.xml
ids.xml
<resources>
<item name="counterTextView" format="integer" type="id"/>
</resources>
我正在尝试学习 Anko
的布局设计。我已经使用 Anko
设置了布局,如下所示
relativeLayout {
val counterTextView = textView {
text = "0"
textSize = 24f
}
button {
onClick {
count++
counterTextView.text = count.toString()
}
}.lparams {
below(counterTextView)
}
}
我只需要一个带有 TextView 和 TextView 下方按钮的相对布局。
以上代码报错如下
Caused by: org.jetbrains.anko.AnkoException: Id is not set for android.widget.TextView{d8dedcd V.ED..... ......ID 0,0-0,0}
at org.jetbrains.anko.RelativeLayoutLayoutParamsHelpersKt.below(RelativeLayoutLayoutParamsHelpers.kt:60)
at com.example.app.MainActivity.onCreate(MainActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
有人知道怎么解决吗?
我想如果你想使用 relativeLayout
参数,你必须为视图创建 id,所以试试这个
relativeLayout {
val counterTextView = textView {
id = R.id.counterTextView // add this line
text = "0"
textSize = 24f
}
button {
onClick {
count++
counterTextView.text = count.toString()
}
}.lparams {
below(counterTextView)
}
}
并且您必须在值文件夹
中创建ids.xml
ids.xml
<resources>
<item name="counterTextView" format="integer" type="id"/>
</resources>