Only the original thread that created a view hierarchy can touch its views

来源:互联网 发布:工程印花税怎么算法 编辑:程序博客网 时间:2024/05/22 12:19

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


该错误是由于更新UI界面的操作放在了子线程执行。


解决方法:使用Handler将更新UI界面的操作pull到主线程中。


例如:

private Handler handler = new Handler() {
@Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            et_epc.setText(msg.obj.toString());
            ArrayList<String> getEpc = handle.getAllEpc();
            String readEpc = msg.obj.toString();
            for(int i = 0; i < getEpc.size(); i++)
            {
            if(readEpc.equals(getEpc.get(i)))
                {
                Info info = handle.getInfoByEpc(readEpc);
                    et_name.setText(info.getName());
                    et_repair.setText(info.getRepair());
                }
            else 
            {
            et_name.setText("");
            et_repair.setText("");
            }
            }
        }
    };


1 0
原创粉丝点击