【发布时间】:2015-12-11 15:08:33
【问题描述】:
我遇到了一个问题,即一个线程在其处理程序初始化之前尝试将消息发送到另一个线程的处理程序。这种异步线程通信很容易导致空指针异常。
我正在尝试使用以下代码来解决此问题(等待通知算法),但我不明白如何从发送消息的线程中调用 getHandler(),因为我不断收到“非静态方法不能从静态上下文中调用”错误。
尝试修复消息接收线程的代码:
public class LooperThread extends Thread {
private static Handler mHandler;
public void run() {
Looper.prepare();
synchronized (this) {
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
notifyAll();
}
Looper.loop();
}
public synchronized Handler getHandler() {
while (mHandler == null) {
try {
wait();
} catch (InterruptedException e) {
//Ignore and try again.
}
}
return mHandler;
}
}
当我尝试以下代码时,我不断收到“无法从静态上下文编译器错误中调用非静态方法。
消息发送线程:
public class SenderThread extends thread{
private static Handler senderHandler;
public void run(){
Looper.prepare();
senderHandler = LooperThread.getHandler(); //This is where the error occurs!
//do stuff
senderHandler.msg(obj);
Looper.loop();
}
}
我知道我可能不应该尝试在 run() 方法中初始化发送者线程的处理程序,因为它会被重复调用,因此会很浪费。 我应该从哪里调用 LooperThread 的 getHandler() 方法?
背景信息:
我使用了这个问题和其中一个答案作为参考:How do I ensure another Thread's Handler is not null before calling it?
【问题讨论】:
标签: java android multithreading handler