I have a Chronometer
class with the following parameters
open class Chronometer @JvmOverloads constructor(context: Context?,
attrs: AttributeSet? = null,
defStyle: Int = 0)
And a MyHandler
class that calls 2 methods from the Chronometer
class. For that, I need to pass a Context
, but I don't know what context should it be.
class MyHandler(activity: Chronometer?) : Handler() {
Chronometer(CONTEXT HERE).updateText(SystemClock.elapsedRealtime())
Chronometer(CONTEXT HERE).dispatchChronometerTick()
sendMessageDelayed(
Message.obtain(this, TICK_WHAT), 0)
}
I have tried ChronoFragment().context
(ChronoFragment is a fragment class), MainActivity().applicationContext
and MainActivity().baseContext
but all of those throw a NullPointerException:
Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
那么,这里传递的上下文应该是什么?谢谢。