简体   繁体   English

无法在Handler()内未调用Looper.prepare()的线程内创建处理程序

[英]Can't create handler inside thread that has not called Looper.prepare() inside Handler()

I am calling a Handler class from a background thread. 我正在从后台线程调用Handler类。 In the Handler class, I am trying to display a toast. 在Handler类中,我尝试显示吐司。 Theoratically it should work flawlessly because Handler is the Queue that forwards the UI tasks to the main UI thread. 从理论上讲,它应该可以正常工作,因为Handler是将UI任务转发到主UI线程的Queue。 However, in my case the I am getting exception. 但是,就我而言,我正在例外。

private void firstTimeLogin() {

        final LoginUiThreadHandler loginHandler = new LoginUiThreadHandler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message m = loginHandler.obtainMessage();

                Bundle bund = new Bundle();
                bund.putInt("loginResult", 1);
                m.setData(bund);

                loginHandler.handleMessage(m);
            }
        }).start();
    }

    private class LoginUiThreadHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            int loginResult = msg.getData().getInt("loginResult");
            if(loginResult == 0) 
                Toast.makeText(getActivity().getApplicationContext(), "Login success", Toast.LENGTH_SHORT).show();

        }
    }

What am I doing wrong? 我究竟做错了什么?

Replace with - 用。。。来代替 -

LoginUiThreadHandler loginHandler = new LoginUiThreadHandler(Looper.getMainLooper());

instead of- 代替-

LoginUiThreadHandler loginHandler = new LoginUiThreadHandler();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在未调用Looper.prepare()的线程内创建处理程序 - Can not create handler inside thread that has not called Looper.prepare() Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android handler: Can't create handler inside thread that has not called Looper.prepare() Handler或runOnUiThread解决方案“无法在未调用Looper.prepare()的线程内创建处理程序” - Handler or runOnUiThread solution for “Can't create handler inside thread that has not called Looper.prepare() ” 无法在 AsyncTask 内未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask 无法在未调用Looper.prepare()的线程内创建处理程序 - 对话框中的AsyncTask - Can't create handler inside thread that has not called Looper.prepare() - AsyncTask inside a dialog 无法在未在AsyncTask for ProgressDialog中调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在警告对话框线程中未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread 无法在尚未调用looper.prepare的线程内创建处理程序 - cant create handler inside thread that has not called looper.prepare 无法在未调用looper.prepare()的线程内创建处理程序 - cannot create handler inside thread that has not called looper.prepare()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM