Reputation: 1307
I'm trying to add a button to my activity using anko. I know is possible using RxJava (I've done that). I want to know how to do that using Anko and asynchronous. The code works when is running on a synchronous thread.
class MainActivity : AppCompatActivity() {
fun someMethod(){
async(UI) {
bg {
var button = Button(this@MainActivity)
button.background = [email protected](R.drawable.ic_action_balloon)
myFrame.addView(button)
}
}
}
When I build the code, I get this error:
Error:(32, 17) Val cannot be reassigned
Error:(32, 55) Unresolved reference: getDrawable
Upvotes: 1
Views: 251
Reputation: 35
You can only modify Views from the UI thread (bg runs on a different thread).
I tried your code, it compiles but does nothing.
Also, if you don't want to return anything from that coroutine, use launch instead of async.
Anyway, drop the bg and it should work. Didn't you, by chance, mean to do something like this?
launch(UI) {
val someJob = bg {
//Do some work here
someResult
}
doWhateverWithUI(someJob.await())
}
Upvotes: 0
Reputation: 2636
The function activity.getDrawable
was introduced in the API 21. You should use activity.resources.getDrawable
instead.
Upvotes: 1