`

CalledFromWrongThreadException

阅读更多

今天写一个进度条更新程序,遇到这个异常

网上go之发现解决方案如下:

1:利用Handler

 

Handler textHandle = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				//do something
			}
};

new Thread(){
  public  void run(){
	textHandle.sendMessage(textHandle.obtainMessage());
  }
}

 2:利用 runOnUiThread

 runOnUiThread(new Runnable() {

 runOnUiThread(new Runnable() {
 public void run(){}
}

 

3:利用Handler (未验证过)

Handler refresh = new Handler();
refresh.post(new Runnable() {
    public void run()
    {
         doSomething
    }
});

 4:利用AsyncTask (未验证过)

 

后想起Swing多线程中,也有一个GUI 线程模型类似,对UI的修改只能在特定的线程里处理,看来很多模式其实都是一样的,毕竟是经过血的教训提炼出来的模式。

分享到:
评论

相关推荐

    问题:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original

    问题:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a 我的场景是 LinearLayout.LayoutParams params11 = new LinearLayout.LayoutParams(264, LinearLayout....

    【Android】CalledFromWrongThreadException 深入源码分析

    先上结论 出现此问题的原因是:在非 UI 线程中创建了 Dialog,而在 UI 线程中调用了 show() 方法 问题还原 在使用 dialog 的时候,因为线程问题,在调用 dismiss() 方法的时候,出现如下常见的 crash–Only the ...

    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 ...

    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更新的几种方法

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

    开新线程引发的常见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 ...

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

    throw new CalledFromWrongThreadException( Only the original thread that created a view hierarchy can touch its views.); } } 一般情况下在子线程更新UI是会报错的,因为在ViewRootImpl

Global site tag (gtag.js) - Google Analytics