Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:10:58+00:00 2026-06-09T05:10:58+00:00

Given this code: public class MainActivity extends FragmentActivity implements ActionBar.TabListener { public static final

  • 0

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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:10:59+00:00Added an answer on June 9, 2026 at 5:10 am

    Try using a WeakReference, as described in this article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this code: public class Messager implements Runnable { public static void main(String[] args)
Given this code: public class Car { public virtual int CarId { get; set;
Given this snippet of code public abstract class Foo { private static SqlConnection _sqlConnection;
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
Given this code public interface IRepository<T> { IQueryable<T> GetAll(); } and this public class
given this code namespace GridTests { public class Grid<T> { IEnumerable<T> DataSource { get;
Given this code: class Base{ public: Base(); virtual ~Base(); }; class Derived: protected Base{
Given this code: class base { public: string foo() const; // Want this to
Given this code: #include <iostream> using namespace std; class Foo { public: Foo ()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.