必须初始化变量“runnable”
为什么 Kotlin 会抱怨这一点:
class MyActivity : Activity {
private var handler:Handler = Handler()
private var runnable: Runnable = Runnable {
/* Do something very important */
handler.postDelayed(this@MyActivity.runnable, 5000)
}
}
编译器抱怨说,在 Line 中,它是由处理程序再次发布的。这在普通的Java中确实有效:Variable 'runnable' must be initialized
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable, 5000);
}
};