This Handler class should be static or leaks may occur (null)
Is the 'class' this message referring to 'MyActivity' here, since Handler is an object and i did declare it static. Should I ignore it or there something I should add, like 'static' somewhere in the 'MyActivity' declaration (I tried this and got errors). I notice 'WeakReference' is often suggested for this lint warning.
public class MyActivity extends Activity{
...
static Handler handler;
...
handler = new Handler()
{
public void handleMessage(Message msg) {
You declared the data member to be static. However, you are using anonymous inner class, and therefore your subclass of
Handler
is notstatic
.Instead of:
use:
where
MyVeryOwnHandler
is either a regular Java class or astatic
inner class:Note that the error is that the class needs to be
static
; it does not say that the object needs to bestatic
.