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

來源:互聯網
上載者:User

經常遇到這個錯誤

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

這是因為在背景工作執行緒更新了UI,可以使用Handle來處理。

 

如何區分:

Log.d(TAG, "
original
thread "+Looper.getMainLooper().getThread().getId());

Log.d(TAG, "currrent

thread "+Thread.currentThread().getId());

 

如果你對於Android的Thread+Handler方式感覺繁瑣,不妨試試Activity提供的另外一種簡單的方法runOnUiThread,runOnUiThread可以協助你線上程中執行UI更新操作,我們只需要線上程中寫上類似

MyActivity
.this.runOnUiThread(new Runnable() {
                    @Override
                        public void run() { 

                           // refresh ui 的作業碼

                        }
                    });

  這裡需要注意的是runOnUiThread是Activity中的方法,線上程中我們需要告訴系統是哪個activity調用,所以前面顯示的指明了activity。

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.