Android 开发问题:Only the original thread that created a view hierarchy can touch its views.

📅 2026/6/30 3:01:14
Android 开发问题:Only the original thread that created a view hierarchy can touch its views.
在 Android 开发中应用运行时出现如下错误信息FATAL EXCEPTION: pool-2-thread-1 Process: com.my.devicecontroller, PID: 8042 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.问题原因# 解读 android.view.ViewRootImpl$CalledFromWrongThreadException: 只有创建了视图层次结构的原始线程才能操作其视图在 Android 开发中UI 线程主线程是唯一有权创建和更新用户界面的线程上述错误信息中在工作线程pool-2-thread-1中直接操作了 UI处理策略使用 runOnUiThreadrunOnUiThread(()-{// 操作 UI});使用 HandlernewHandler(Looper.getMainLooper()).post(()-{// 操作 UI});