Android – this handler class should be static, otherwise it may leak (null)

This Handler class should be static or leaks may occur (null)

The 'class' of this message refers to' myactivity ', because the handler is an object, and I do declare it static. Should I ignore it or add some content, such as' static' in the 'myactivity' declaration (I tried this and made an error). I notice that this kind of lint warning usually recommends using 'WeakReference'

public class MyActivity extends Activity{
...
static Handler handler;
...
handler = new Handler()
  {
    public void handleMessage(Message msg) {

resolvent:

You declare that the data member is static. However, you use an anonymous inner class, so your handler subclass is not static

Substitute:

  handler = new Handler() {
    public void handleMessage(Message msg) {
      // do cool stuff
    }
  };

use:

handler=new MyVeryOwnHandler();

Where myveryownhandler is a regular Java class or a static inner class:

private static class MyVeryOwnHandler extends Handler {
  public void handleMessage(Message msg) {
      // do cool stuff
  }
};

Please note that the error is that this class needs to be static; It does not say that objects need to be static

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>