Android:android.view.ViewRoot$CalledFromWrongThreadException - 如何解决问题? [英] Android:android.view.ViewRoot$CalledFromWrongThreadException - How to solve the problem?

查看:15
本文介绍了Android:android.view.ViewRoot$CalledFromWrongThreadException - 如何解决问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发的一个应用程序正在与服务器进行通信,并且通信进程在其自己的线程中运行.有异步调用 - 例如 login() 和 onLoginResponse().

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 login() and onLoginResponse().

login() 在主活动中调用,响应也在主活动中处理(onLoginResponse()).在 onLoginResponse() 方法中有修改布局元素的 updateGUIState() 方法:

login() is called in the main activity and the response is handled in main activity as well (onLoginResponse()). In onLoginResponse() method there is updateGUIState() method which modifies layout elements:

private void updateGUIState() {

    Log.i(TAG, "executing updateGUIState");

    arrangeLayoutElements();
    txtTime.setText(mStrRecordingTime);
    if (settings.isRecording()) {
        //btnAction.setText("Stop");
        btnAction.setImageResource(R.drawable.button_stop);
    } else {
        //btnAction.setText("Capture");
        btnAction.setImageResource(R.drawable.button_record);
    }

    //set privacy level text
    if (settings.getPrivacyLevel() == 0) {
        txtPrivacyLevel.setText("Private");
    } else if (settings.getPrivacyLevel() == 1) {
        txtPrivacyLevel.setText("Public");
    }

    if (settings.isMute()) {
        muteIcon.setIconImage(R.drawable.ic_volume_off_small);
    } else {
        muteIcon.setIconImage(R.drawable.ic_volume_small);
    }

    if (mIsUploading) {
        txtUploadingText.setVisibility(View.VISIBLE);
        uploadingProgressBar.setVisibility(View.VISIBLE);
    } else {
        txtUploadingText.setVisibility(View.INVISIBLE);
        uploadingProgressBar.setVisibility(View.INVISIBLE);
    }

    if (mEncoderConnection != null) {
        txtConnectionStatus.setText("Connected");
    } else {
        txtConnectionStatus.setText("Disconnected");
    }
}

当执行到达此方法时(从 onLoginResponse() 调用时),应用程序崩溃并且日志显示以下消息:

When the execution reaches this method (when called from onLoginResponse()) the application crashes and the log displays the following message:

android.view.ViewRoot$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图.

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

有谁知道如何在修改布局和修复问题之前修改逻辑以切换到适当的线程?

Does anyone know how it is possible to modify the logic in order to switch to appropriate thread before modifying the layout and fix the problem?

谢谢!

推荐答案

尝试 Handler.

onLoginResponse() 是回调函数吗?
如果是,则问题可以通过Handler解决.

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

onLoginResponse()中,

hRefresh.sendEmptyMessage(REFRESH);


    Handler hRefresh = new Handler(){
    @Override
    public void handleMessage(Message msg) {
    switch(msg.what){
         case REFRESH:
                /*Refresh UI*/
                updateGUIState();
                break;
       }
    }
};

这篇关于Android:android.view.ViewRoot$CalledFromWrongThreadException - 如何解决问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆