Android:android.view.ViewRoot$CalledFromWrongThreadException - How to solve the problem?

前端 未结 3 495

An application I am currently developing is communicating with the server and the communication process runs in its own thread. There are asynchronous calls - for example lo

相关标签:
3条回答
  • 2020-12-06 14:25

    updateGUIState() needs to be run on the UI thread. A possible solution is to implement your GUI update in a Runnable, and call the runOnUiThread method with your runnable.

    0 讨论(0)
  • 2020-12-06 14:34

    Try Handler.

    Is onLoginResponse() is a callback function?
    If it is, the problem can be solved by Handler.

    In onLoginResponse(),

    hRefresh.sendEmptyMessage(REFRESH);
    
    
        Handler hRefresh = new Handler(){
        @Override
        public void handleMessage(Message msg) {
        switch(msg.what){
             case REFRESH:
                    /*Refresh UI*/
                    updateGUIState();
                    break;
           }
        }
    };
    
    0 讨论(0)
  • 2020-12-06 14:41

    To add to bhatt4982's response, you can also call handler.post(onLoginThread), where onLoginThread is a Thread whose runnable will run inside the GUI thread.

    0 讨论(0)
提交回复
热议问题