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

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

问题描述

我目前正在开发与服务器和通信进程通信的应用程序在自己的线程中运行。有异步调用 - 例如登录()和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().

登录()被调用的主要活动和响应在主要活动处理,以及(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?

谢谢!

推荐答案

尝试处理程序

onLoginResponse()是一个回调函数?
如果是,该问题可以通过处理程序来解决。

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.view.ViewRoot $ CalledFromWrongThreadException - 如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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