从线程调用 Handler 会导致 NullPointerException

发布于 2024-11-28 08:26:04 字数 1300 浏览 2 评论 0原文

我需要从 Thread 调用 Handler,这是我的 Thread

new Thread(){
    public void run(){
        try{
            Thread.sleep(2000); 
        }
        catch(Exception ex){}

        actHandler.sendEmptyMessage(0);
    }}.start();

我像这样调用 Handler有时

actHandler=new Handler(){
    public void handleMessage(android.os.Message msg){

    }
};

它工作正常,有时它会导致该行出现 NullPointerException actHandler.sendEmptyMessage(0);

这是我的所有代码:

public class Casinos extends Activity {
    ProgressDialog pd;
    Handler actHandler;

    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        pd=ProgressDialog.show(this,"","Please wait...");
        new Thread(){
            public void run(){
            try{
                Thread.sleep(2000); 
            }
            catch(Exception ex){}

            actHandler.sendEmptyMessage(0);
        }}.start();

        setContentView(R.layout.casinos);

        actHandler = new Handler(){
        public void handleMessage(android.os.Message msg){
            super.handleMessage(msg);
                pd.dismiss();
        }};

    }

}

该怎么办?

提前致谢。

I need to call the Handler from a Thread, Here is my Thread:

new Thread(){
    public void run(){
        try{
            Thread.sleep(2000); 
        }
        catch(Exception ex){}

        actHandler.sendEmptyMessage(0);
    }}.start();

I'm calling the Handler like this

actHandler=new Handler(){
    public void handleMessage(android.os.Message msg){

    }
};

Some times its working fine and some times it leads to a NullPointerException at the line
actHandler.sendEmptyMessage(0);

Here is all my code:

public class Casinos extends Activity {
    ProgressDialog pd;
    Handler actHandler;

    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        pd=ProgressDialog.show(this,"","Please wait...");
        new Thread(){
            public void run(){
            try{
                Thread.sleep(2000); 
            }
            catch(Exception ex){}

            actHandler.sendEmptyMessage(0);
        }}.start();

        setContentView(R.layout.casinos);

        actHandler = new Handler(){
        public void handleMessage(android.os.Message msg){
            super.handleMessage(msg);
                pd.dismiss();
        }};

    }

}

What to do?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

源来凯始玺欢你 2024-12-05 08:26:04

尝试这样:

Message msg = new Message();
msg.arg1 = int value;
actHandler.sendMessage(msg);

在您的情况下,如果处理消息队列的循环器正在退出,那么它将返回失败。

try like this:

Message msg = new Message();
msg.arg1 = int value;
actHandler.sendMessage(msg);

In your case if the looper processing the messsage queue is exiting then it will return failure.

雨夜星沙 2024-12-05 08:26:04

您可能在 new Thread() 语句之后实例化了 actHandler

actHandler = new Handler();

请向我们展示更多代码来验证,但情况可能就是这样。

解决方案

您已在线程声明后初始化了actHandler

public class Casinos extends Activity {
    ProgressDialog pd;
    Handler actHandler;
    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        pd=ProgressDialog.show(this,"","Please wait...");
        //move this HERE!!
        actHandler=new Handler(){
            public void handleMessage(android.os.Message msg)
            {
                super.handleMessage(msg);
                pd.dismiss();
            }
        };

        new Thread(){
            public void run(){
                try{
                    Thread.sleep(2000); 
                }
                catch(Exception ex){}
                actHandler.sendEmptyMessage(0);
            }
        }.start();
        setContentView(R.layout.casinos);
    }
}

You may have instantiated actHandler after the new Thread() statement.

actHandler = new Handler();

Please show us some more code to verify but this is probably the case.

SOLUTION

You have initialised actHandler after the Thread declaration

public class Casinos extends Activity {
    ProgressDialog pd;
    Handler actHandler;
    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        pd=ProgressDialog.show(this,"","Please wait...");
        //move this HERE!!
        actHandler=new Handler(){
            public void handleMessage(android.os.Message msg)
            {
                super.handleMessage(msg);
                pd.dismiss();
            }
        };

        new Thread(){
            public void run(){
                try{
                    Thread.sleep(2000); 
                }
                catch(Exception ex){}
                actHandler.sendEmptyMessage(0);
            }
        }.start();
        setContentView(R.layout.casinos);
    }
}
未央 2024-12-05 08:26:04

在哪里初始化 actHandler

如果是在其他线程中,请确保之前调用了初始化。 (最好的办法可能是尽快初始化)

Where do you initialize your actHandler?

If it is in another thread, be sure that the initialization is called before. (the best thing to do may be an initialization as soon as you can)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文