Mengatasi ‘constructor Handler()’ is deprecated

aldan
1 min readMay 10, 2021

Bagi Kamu yang menggunakan Kotlin sebagai bahasa pemograman untuk Android mungkin akan menemukan warning sebagai berikut :

‘constructor Handler()’ is deprecated. Deprecated in Java

android.os.Handler @Deprecated
public Handler()
Default constructor associates this handler with the Looper for the current thread. If this thread does not have a looper, this handler won’t be able to receive messages so an exception is thrown.

Deprecated
Implicitly choosing a Looper during Handler construction can lead to bugs where operations are silently lost (if the Handler is not expecting new tasks and quits), crashes (if a handler is sometimes created on a thread without a Looper active), or race conditions, where the thread a handler is associated with is not what the author anticipated. Instead, use an java.util.concurrent.Executor or specify the Looper explicitly, using Looper.getMainLooper, {link android.view.View#getHandler}, or similar. If the implicit thread local behavior is required for compatibility, use new Handler(Looper.myLooper()) to make it clear to readers.
< Android API 30 Platform >

Bagaimana mengatasinya? Rubah Handler() menjadi Handler(Looper.getMainLooper())

Semoga bermanfaat, Terima kasih

--

--

aldan

ⓘ I set time limit for this app per day. User is temporarily inactive.