Alan Moment
  • Introduction
  • 智慧家庭
    • Fibaro 系統整合便宜的 IP Cam
  • Life
    • 用AWS Glacier做最後的冷資料備份
    • 如何在macOS修改影音檔日期
  • 3D Printer
    • Atom2.5EX 之血淚組裝
    • 列印經驗紀錄
      • SpoolHolder
    • 製圖經驗
      • Turntable
      • 重製Atom3散熱風扇
    • 線材經驗
      • PETG
        • 首測
  • Kubernetes
    • 使用Kops建立Kubernetes
    • 使用HelmV2
    • Kubernetes的技術問題排解技巧
  • PHP
    • 管理PHP Library的利器Composer
    • PHP安裝JSON
    • Phalcon首發
    • Gearman Job Worker for PHP
    • Laravel 首發 !!!
    • Data Encrypt & Decrypt
  • Python
    • Django + Python 開發環境建置
  • Android
    • Android zipcode library of maven
    • Android use foreign object of OrmLite
    • ProgressBar while loading ListView of Android
    • AsyncTask download image by the Android
    • Use Thread control Android UI
    • Android Universal Image Loader
  • Ruby on Rails
    • Install rmagick on the Windows of Ruby on Rails
    • Ruby on Rails deploy on Heroku
    • Ruby on Rails 小問題
  • React
    • Ditched AngularJS for React
  • Tessel
    • 很潮的 Tessel
    • Connect to Slack on Tessel
    • Baby Help on Tessel
  • Node.js
    • CentOS 安装 Node.js 0.8.5
  • OOAD
    • Injection Principle Design Pattern
  • Linux
    • SSH免密碼登入遠端電腦
    • Apache與Tomcat的結合
    • The bash auto build
  • Hadoop
    • CentOS 5.5 + Hadoop 0.20
    • CentOS 5.5 + Hbase 0.94.8
    • Hadoop + Hbase 叢集環境
    • Hadoop 溝通橋梁之 Thrift 0.7
    • 使用MapReduce之替代方案Hive
    • 使用Sqoop將MySQL資料匯入Hbase
  • Database
    • 吃足苦頭的Mssql
  • IDE
    • Netbeans console中文亂碼解決方法
    • 用NetBeans開發Ruby On Rails
  • Agile
    • 淺談我的Agile
  • 協作工具
    • 建置專屬自己的Github之Gitlab
    • Gitlab 4.1 upgrade to Gitlab 6.0超偷懶方法
    • Install Phabricator and run on the Gitlab
    • Phabricator 基本應用
    • Phabricator review code應用
    • Redmine之基本建置與Scrum應用
    • Omnibus Gitlab 7 基礎操作
    • Git Push Notify to Slack on Gitlab
    • phabricator-extensions-Sprint 無法抓到正確的 Story Points
  • 其他
    • 慶祝Octopress開張
    • 走在時尚的尖端! Ghost
    • 大搬家
    • 網頁教學初體驗
    • 網路攻擊很猖狂
Powered by GitBook
On this page

Was this helpful?

  1. Android

Use Thread control Android UI

在開發Android app若要用Thread改變UI的話會遇到這個Exception

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4637)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:876)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4174)
at android.view.View.invalidate(View.java:10313)

原因是Android不允許onCreate之後再建立Thread改變UI,所以得透過他提供的方法

// run on ui thread
runOnUiThread(new Runnable() {
    public void run() {
        // TODO
    }
});

這樣就可以透過Thread來執行改變UI的事件了。

完整的範例如下,透過Thread 5秒後改變某個UI的屬性

// Start thread
@Override
protected void onResume()
{
    super.onResume();
    myThread.start();
}

// Stop thread
@Override
public void onPause()
{
    super.onPause();
    myThread.interrupt();
}

// Execute thread after 5s.
final Thread myThread = new Thread() {
    public void run() {
        try {
            this.sleep(5000);
            // run on ui thread
            runOnUiThread(new Runnable() {
                public void run() {
                    // TODO
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

Nov 11th, 2013 11:04:00am

PreviousAsyncTask download image by the AndroidNextAndroid Universal Image Loader

Last updated 3 years ago

Was this helpful?