runOnUiThread 没有调用
runOnUiThread is not calling
localChatManager.addIncomingListener { from, message, chat ->
Log.v(TAG,"listener")
//You can't modify views from non-UI thread.
this@chatActivity.runOnUiThread { object :Runnable{
override fun run() {
Log.i(TAG,"runOnUiThread")
}
} }
}
我无法弄清楚为什么 runOnUiThread 无法正常工作,但在该方法之外一切正常。
您正在做的是将 lambda 传递给 runOnUiThread
函数。它将 运行 那个 lambda,并创建一个继承自 Runnable
的 object
,然后什么也不做。如果像这样格式化(添加了一些额外的日志语句和解释),也许你会看到更好一点:
runOnUiThread({
Log.i(TAG, "This is run")
object : Runnable { // This whole expression
override fun run() { // returns an object which
Log.i(TAG, "runOnUiThread") // is a Runnable, but does
} // not at any point invoke
} // its "run" method
Log.i(TAG, "And so is this")
})
创建的object
没有分配给变量,也从未使用过。如果要将 Runnable
实例传递给 runOnUiThread
方法,只需将其放在 runOnUiThread
调用的括号内即可:
runOnUiThread(
object : Runnable {
override fun run() {
Log.i(TAG, "runOnUiThread")
}
}
)
使用 运行OnUiThread 的最简单方法是使用 SAM 转换将 lambda 传递给它,然后直接在其中编写要执行的代码。
runOnUiThread {
Log.i(TAG, "runOnUiThread")
}
这是 official documentation covering SAM conversions,它恰好在其示例中使用了 Runnable
。
以上回答正确,采纳
如果您来自 Java,这里是您代码的等效 Java 示例:
runOnUiThread(new Runnable() { // This runnable is created
@Override // from lambda by SAM convention
public void run() {
new Runnable() { // This Runnable is instantiated
@Override // inside the lambda but never runs.
public void run() {
Log.i(TAG, "runOnUiThread");
}
};
}
});
我希望你能看到内部代码没有被执行的原因。
基于 Rx 的答案:
import rx.Observable
Observable.just(true)
.observeOn(AndroidSchedulers.mainThread())
.subscribe{
// your code
}
使用协程更好
尝试使用
runBlocking (Dispatchers.Main) {
// any Main thread needs
}
localChatManager.addIncomingListener { from, message, chat ->
Log.v(TAG,"listener")
//You can't modify views from non-UI thread.
this@chatActivity.runOnUiThread { object :Runnable{
override fun run() {
Log.i(TAG,"runOnUiThread")
}
} }
}
我无法弄清楚为什么 runOnUiThread 无法正常工作,但在该方法之外一切正常。
您正在做的是将 lambda 传递给 runOnUiThread
函数。它将 运行 那个 lambda,并创建一个继承自 Runnable
的 object
,然后什么也不做。如果像这样格式化(添加了一些额外的日志语句和解释),也许你会看到更好一点:
runOnUiThread({
Log.i(TAG, "This is run")
object : Runnable { // This whole expression
override fun run() { // returns an object which
Log.i(TAG, "runOnUiThread") // is a Runnable, but does
} // not at any point invoke
} // its "run" method
Log.i(TAG, "And so is this")
})
创建的object
没有分配给变量,也从未使用过。如果要将 Runnable
实例传递给 runOnUiThread
方法,只需将其放在 runOnUiThread
调用的括号内即可:
runOnUiThread(
object : Runnable {
override fun run() {
Log.i(TAG, "runOnUiThread")
}
}
)
使用 运行OnUiThread 的最简单方法是使用 SAM 转换将 lambda 传递给它,然后直接在其中编写要执行的代码。
runOnUiThread {
Log.i(TAG, "runOnUiThread")
}
这是 official documentation covering SAM conversions,它恰好在其示例中使用了 Runnable
。
以上回答正确,采纳
如果您来自 Java,这里是您代码的等效 Java 示例:
runOnUiThread(new Runnable() { // This runnable is created
@Override // from lambda by SAM convention
public void run() {
new Runnable() { // This Runnable is instantiated
@Override // inside the lambda but never runs.
public void run() {
Log.i(TAG, "runOnUiThread");
}
};
}
});
我希望你能看到内部代码没有被执行的原因。
基于 Rx 的答案:
import rx.Observable
Observable.just(true)
.observeOn(AndroidSchedulers.mainThread())
.subscribe{
// your code
}
使用协程更好
尝试使用
runBlocking (Dispatchers.Main) {
// any Main thread needs
}