android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
来源:互联网 发布:重返狼穴3mac怎么安装 编辑:程序博客网 时间:2024/06/05 15:58
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 = new Handler(){ public void handleMessage(Message msg){ switch (msg.what){ case UPDATE_TEXT: //在这里进行UI更新 mTextView.setText("123456"); break; default: break; } }};@Overrideprotected 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(); } });}
阅读全文
0 0
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy
- CalledFromWrongThreadException: Only the original thread that created a view
- CalledFromWrongThreadException: Only the original thread that created a view
- android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view
- android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view h
- 16-07-24 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that
- CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- java反射机制
- ndarray数组的运算
- Xcode 左边导航栏符号含义详解
- composer 报 zlib_decode(): data error
- 【OpenCV入门指南】第三篇Canny边缘检测
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
- 算法常考题目
- [2017纪中10-31]Tree 构造
- C++实现三个矩阵乘法
- JavaScript知识整理
- 排序算法和搜索算法
- [日推荐]麻麻再也不用担心你忘记密码啦!
- F
- 末日的传说 洛谷p1338