`
v5browser
  • 浏览: 1137010 次
社区版块
存档分类
最新评论

Only the original thread that created a view hierarchy can touch its views错误相关

 
阅读更多
很多网友在Android中使用多线程处理UI相关内容时可能会发现Logcat提示Only the original thread that created a view hierarchy can touch its views这样的错误,这主要是Android的相关View和控件不是线程安全的,我们必须做独立的处理这点比J2ME麻烦一些,这里Android给我们提供了很多方法,有关线程的,我们需要了解下J2ME中一些传统的线程创建方法,比如Runnable或直接new Thread(),大家需要了解UI线程、worker线程以及一些概念。今天android开发网说一种简单的方法除了异步任务AsyncTask外使用Handler可以很好的处理,和Win32的消息很像。

首先我们需要明白,主线程或者这里说的原始线程original thread 一般情况下是UI线程,当然UI线程并不一定是主线程,我们不能长时间的阻塞该应用,在Android平台上可能会产生类似Force close或Wait这样的对话框这里我们成为ANR,这里除了使用ProgressDialog方式给用一个动态的进度代表当前处理并没有中断可能需要一些时间,所以android123告诉大家相关的网络处理可以使用工作者线程,但是worker线程不能处理显示元素即UI相关的View或Widget包中的高层的控件,所以通过一个Handler对象可以很好的传递Runnable或Message ,下面我们用一个简单的例子来描述

final Handler cwjHandler = new Handler();

final Runnable mUpdateResults = new Runnable() {
public void run() {
updateUI();
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

NetworkOperation();//一个很费时间的I/O操作,比如网络或文件读写等等。

}

protected void NetworkOperation() {

Thread t = new Thread() {
public void run() {
doSomething(); //处理得到结果了,这里一些内容保存在主类的成员变量中
cwjHandler.post(mUpdateResults); //高速UI线程可以更新结果了
}
};
t.start();
}

private void updateUI() {

//详细的更新
}

有关Java以及Android的线程,我们将在以后的内容中介绍到,本次由于android123@163.com 邮箱中的相关内容讲到这些,以后我们会告诉大家一些线程同步以及死锁问题的解决方法,总体而言感觉相对于Windows Mobile平台,Android上的线程消耗过大,而Symbian的活动对象又相对保守,总的而言Android线程相对简单。

分享到:
评论

相关推荐

    Android studio :后台修改前台界面Only the original thread that created a view hierarchy can touch its views

    Only the original thread that created a view hierarchy can touch its views 这句话的意思是:只有创建这个view的线程才能操作这个view 解决方法就是handle 1、 Handler mHandler; 2、 mHandler = new Handler();...

    Android 子线程更新UI

    android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 这个异常为何不是 Only the main thread that created a view hierarchy ...

    开新线程引发的常见3个异常

    2.android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 3.java.lang.RuntimeException: Can't create handler inside thread ...

    详解Android UI更新的几种方法

    如果是在WT进行UI的更新,则会抛出异常,android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.(只有创建这个View的原

    Android实现在子线程中更新Activity中UI的方法

    本文实例讲述了Android实现在子线程中更新Activity中...ERROR/AndroidRuntime(1222): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its

    Android消息机制基本原理和使用

    例如在子线程进行耗时较长的下载,等下载完成之后,再去更新UI,提示用户下载完成,直接在子线程里更新UI,会得到报错提示:Only the original thread that created a view hierarchy can touch its views。...

    为什么能在子线程通过setText进行更新UI

    为什么可以在子线程通过setText进行更新UI ... Only the original thread that created a view hierarchy can touch its views.); } } 一般情况下在子线程更新UI是会报错的,因为在ViewRootImpl

    Android View Hierarchy,解决View Hierarchy不能启动

    解决这个启动 View Hierarchy时的这个bug: hierarchy viewer unable to debug device

    【Android】CalledFromWrongThreadException 深入源码分析

    在使用 dialog 的时候,因为线程问题,在调用 dismiss() 方法的时候,出现如下常见的 crash–Only the original thread that created a view hierarchy can touch its views.,堆栈信息如下: threadName:main,...

    ViewServer-解决View Hierarchy不能启动

    解决The standalone version of hieararchyviewer is deprecated.问题的文件,有源码,也有打好的apk文件

    iOS.Programming.The.Big.Nerd.Ranch.Guide.5th.Edition.01

    Views and the View Hierarchy Chapter 4. Text Input and Delegation Chapter 5. View Controllers Chapter 6. Programmatic Views Chapter 7. Localization Chapter 8. Controlling Animations Chapter 9. ...

    The Elements of Computing Systems

    Unlike other texts that cover only one aspect of the field, The Elements of Computing Systems gives students an integrated and rigorous picture of applied computer science, as its comes to play in the...

    hdfview-3.1.0.tar.gz

    The HDFView is a Java-based tool for browsing and editing NCSA HDF4 and HDF5 files. HDFView allows users to browse through any HDF4 and HDF5 file; starting with a tree view of all top-level objects in...

    The Essential Qt 3.3 Class Hierarchy

    The Essential Qt 3.3 Class Hierarchy

    微软内部资料-SQL性能优化3

    Intent locks improve performance because SQL Server examines intent locks only at the table level to determine whether a transaction can safely acquire a lock on that table. This removes the ...

    hierarchy view连接真机调试需要viewserver

    当我们需要在真机上查看当前页面的测量,布局,绘制,发现无法使用minitor.bat工具,只有将viewserver引入到项目中才可以使用,这里我将viewserver做了jar包直接导入项目使用。

    Oracle分区技术介绍

    On the other hand data, that is accessed infrequently, such as old historical data, can be moved to slow disks or stored in subsystems that support a storage hierarchy. Partitioning satisfies this ...

    Memory Systems - Cache, DRAM, Disk

    Memory Systems: Cache, DRAM, Disk is the fi rst book that takes on the whole hierarchy in a way that is consistent, covers the complete memory hierarchy, and treats each aspect in signifi cant detail....

Global site tag (gtag.js) - Google Analytics