Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Calling non-static methods from a static Handler class

Given this code:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

public static final int MESSAGE_NOT_CONNECTED = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
}

// -------------------------------------------------
public final void setStatus(int Rid) {
    final ActionBar actionBar = getActionBar();
    actionBar.setSubtitle(Rid);
}

// -------------------------------------------------
static Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MESSAGE_NOT_CONNECTED:
            setStatus(R.string.title_not_connected);
            break;
        }
    }
}
}

I am getting the compile error: Cannot make a static reference to the non-static method setStatus(int) ...

Which makes sense, because getActionBar() in setStatus() is a non-static method.

I made the Handler class static because of the warning: This Handler class should be static or leaks might occur.

The question: how do I properly access the setStatus() method from within the static handler?

EDIT: new handler code is the answer.

static class hHandler extends Handler {
    private final WeakReference<MainActivity> mTarget;
    hHandler(MainActivity target) {
        mTarget = new WeakReference<MainActivity>(target);
    }

    @Override
    public void handleMessage(Message msg) {
        MainActivity target = mTarget.get();
        If(target == null) {
             return;
        }
        switch (msg.what) {
        case MESSAGE_NOT_CONNECTED:
            target.setStatus(R.string.title_not_connected);
            break;
        }
    }
}
like image 701
vedavis Avatar asked Aug 06 '12 12:08

vedavis


2 Answers

Try using a WeakReference, as described in this article.

like image 60
Alex Lockwood Avatar answered Nov 20 '22 07:11

Alex Lockwood


Since you are now using a WeakReference, mTarget.get() might return null. In your edited code, you are not checking if target is null before executing target.setStatus(R.string.title_not_connected). So this may throw a NullPointerException if the weakreference object has been GC'ed.

like image 45
NickL Avatar answered Nov 20 '22 06:11

NickL