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); }
I get a compilation error: I can not make a static reference to the non-static setStatus (int) method ...
This makes sense because getActionBar () in setStatus () is a non-stationary method.
I made the Handler class static due to a warning: this handler class must be static or a leak may occur.
Question: how to correctly access the setStatus () method from a static handler?
EDIT: The 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; } } }
android handler android-actionbar
vedavis
source share