android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
public class MainActivity extends AppCompatActivity { private Button mButton; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button) findViewById(R.id.btn_button); mTextView = (TextView) findViewById(R.id.tv_show); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Thread(new Runnable() { @Override public void run() { mTextView.setText("1321456"); } }).start(); } }); }
}
這個程式碼表示不能線上程裡邊更新UI,
下面的程式碼展示更新UI 宣告一個靜態常量 ,新增了一個handle物件 重寫handleMessage()
public static final int UPDATE_TEXT = 1; private Button mButton; private TextView mTextView; private Handler handle = newHandler(){ public void handleMessage(Message msg){ switch (msg.what){ case UPDATE_TEXT: //在這裡進行UI更新 mTextView.setText("123456"); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button) findViewById(R.id.btn_button); mTextView = (TextView) findViewById(R.id.tv_show); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Thread(new Runnable() { @Override public void run() { Message message = new Message(); message.what = UPDATE_TEXT; handle.sendMessage(message); } }).start(); } }); }
相關推薦
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
public class MainActivity extends AppCompatActivity { private Button mButton; private TextVi
Android: Only the original thread that created a view hierarchy can touch its views 異常
action 接收 如果 exc void 簡單使用 idg ror ucc 最近自己再寫一個小項目練手,創建一個線程從網絡獲取數據然後顯示在 recyclerView 上。寫好後發現頁面能夠顯示,但是有時候會把請求的數據顯示過來,有時候不會。點開 android moni
Error: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 在子執行緒更新UI 自己的手機型號為榮耀9青春版,不會出現這個問題,可是有的手機會出現這個問題 解決方法
錯誤:Only the original thread that created a view hierarchy can touch its views——Handler的使用(更新UI介面)
今天寫了一個更新UI的小例子,沒想到出了log列印了這樣一個錯誤:Only the original thread that created a view hierarchy can touch its views。goolgle了一下找到了原因。 原來android中相關
子執行緒呼叫invalidate()產生“Only the original thread that created a view hierarchy can touch its views.”原因分析
[TOC] 備註:本文基於sdk28, **ViewActivity頁面禁用了硬體加速(AndroidManifest.xml中添加了android:hardwareAccelerated="false")**。 # 1、異常出處 ViewActivity程式碼連結:https://gitee.com/282
Handler的理解及android.view.ViewRootImpl$CalledFromWrongThreadException錯誤處理
在前面的幾篇文章中,不管是AsyncTAsk方式,亦或是new一個Runable,都沒有涉及UI介面的更新(),今天在進行註冊反饋註冊資訊的時候,就發生了ViewRootImpl$CalledFromWrongThreadException異常,究其原因是因為androi
android之Can't create handler inside thread that has not called Looper.prepare()
好久沒遇到這種錯誤,最初都是因為在新開的執行緒中更新UI才出錯,後來一直沒忘記用handler,也就沒用錯誤,今天有出現如下錯誤,程式碼如下:send.setOnClickListener(new OnClickListener() { @Override
android Fragment java.lang.IllegalStateException:The specified child already has a parent.
在做專案的時候 ,做到了一個Activity裡面有兩個Tab切換,每一個Tab是一個Fragment展示內容,當兩個Tab來回切換的時候,報了一個錯誤 經過查詢原因,原來是Fragment中OnCreateView()的方法呼叫錯了: 正確的方法應該是: 查閱多方
Android-在Handler更新UI報ViewRootImpl$CalledFromWrongThreadException錯誤
今天在寫程式的時候,new Thread()開啟一個子執行緒,在子執行緒裡面通過了Handler的dispatchMessage傳送訊息,在主執行緒中更新了UI後發現報出ViewRootImpl$CalledFromWrongThreadException異常 檢查程式碼流程步驟幾乎
Android 有效地展示圖片(二)Processing Bitmaps Off the UI Thread 在ui執行緒外處理bitmap
原文連結http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html 我們在上節課討論了BitmapFactory.decode系列的方法,但是如果原圖的資料需要從硬碟或者網路或者別的途徑而非記憶
Android View原理解析之基礎知識(MeasureSpec、DecorView、ViewRootImpl)
提示:本文的原始碼均取自Android 7.0(API 24) 前言 自定義View是Android進階路線上必須攻克的難題,而在這之前就應該先對View的工作原理有一個系統的理解。本系列將分為4篇部落格進行講解,本文將主要對MeasureSpec、DecorV
Android閃退:Most only support three sub view
出現的原因是在於smartrefreshlayout巢狀的檢視層級超過三層,現在四層 <com.scwang.smartrefresh.layout.SmartRefreshLayout android:layout_width="match_paren
get_isPlaying can only be called from the main thread unity3d
unity3d報這個錯誤 get_isPlaying can only be called from the main thread. 原因就是多執行緒服務中,非主執行緒操作,ui介面等的一些操作,就
Android View繪制知識問答
oid 不同 無法獲得 code pos ren ecs post 基本 1.View的繪制流程分幾步,從哪開始?哪個過程結束以後能看到view? 答:從ViewRoot的performTraversals開始,經過measure,layout,draw 三個流程
Android View的測量
idt protected 允許 默認 ted onmeasure and htm 三種 一、簡介 Android系統在繪制View前,必須對View進行測量,即告訴系統該畫一個多大的View。這個過程在onMeasure()方法中進行。Android系統提供了Mea
at android.view.LayoutInflater.createViewFromTag的錯誤原因
檢查 mta string bject 報錯 except .com view com 創建對話框時出現下面的錯誤: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘
Android View系統解析(下)
target 過程 getc ets 解包 有時 消息隊列 nbsp 實現 轉載請註明出處:http://blog.csdn.net/singwhatiwanna/article/details/38426471(來自singwhatiwanna的csdn博客)Andr
Android View框架的measure機制
bili llc 過程 posit 完整 http vertical 模式 而且 概述 Android中View框架的工作機制中,主要有三個過程: 1、View樹的測量(measure)Android View框架
談談我對Android View事件分發的理解
event 調用 ack 處理 group ans import ras 運行 寫這篇博客的緣由。近期因為項目中用到相似一個LinearLayout中水平布局中,有一個TextView和Button,然後對該LinearLayout布局設置點擊事件。點擊
android view的 繪制流程
視圖 mtr com androi .com android als 方法 per 韓夢飛沙 韓亞飛 [email protected]/* */ yue31313 han_meng_fei_sha 首先是 從 視圖根 這個類的 進行遍歷 pe