安卓:调用从静态的处理程序类非静态方法静态、程序、方法

2023-09-12 02:32:00 作者:失宠的小丑

鉴于这种code:

 公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener {

公共静态最终诠释MESSAGE_NOT_CONNECTED = 1;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main_layout);
}

// ------------------------------------------------ - 
公众最终无效setStatus(INT RID){
    最后的动作条动作条= getActionBar();
    actionBar.setSubtitle(RID);
}

// ------------------------------------------------ - 
静态处理程序mHandler =新的处理程序(){
    @覆盖
    公共无效的handleMessage(信息MSG){
        开关(msg.what){
        案例MESSAGE_NOT_CONNECTED:
            setStatus(R.string.title_not_connected);
            打破;
        }
    }
}
}
 

我收到编译错误:无法使静态引用非静态方法setStatus(INT)...

这是有道理的,因为getActionBar()的setStatus()是一个非静态方法。

我做了处理程序类的静态因为警告:这个处理程序类应该是静态的或可能发生的泄漏

这样的问题:我该如何正确地从静态处理程序中访问setStatus()方法

编辑:新的处理程序code是答案

 静态类hHandler扩展处理程序{
    私人最终的WeakReference< MainActivity> mTarget;
    hHandler(MainActivity目标){
        mTarget =新的WeakReference< MainActivity>(目标);
    }

    @覆盖
    公共无效的handleMessage(信息MSG){
        MainActivity目标= mTarget.get();
        开关(msg.what){
        案例MESSAGE_NOT_CONNECTED:
            target.setStatus(R.string.title_not_connected);
            打破;
        }
    }
}
 

解决方案

尝试使用的WeakReference ,因为在此描述的 文章 。

一种Android代码依赖分析方案及其在支付宝客户端的应用

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();
        switch (msg.what) {
        case MESSAGE_NOT_CONNECTED:
            target.setStatus(R.string.title_not_connected);
            break;
        }
    }
}

解决方案

Try using a WeakReference, as described in this article.