Android Studio 布局无法预览的问题:Access from Event Dispatch Thread (EDT) is not allowed

xml 复制代码
<!-- 原来是这样 -->


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <Button
        android:id="@+id/btn_go_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="前往测试" />
</androidx.constraintlayout.widget.ConstraintLayout>
xml 复制代码
<!-- 修改成这样 -->

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <Button
        android:id="@+id/btn_go_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="前往测试"
        app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • 对上述布局代码进行修改之后,Android Studio 出现如下错误信息,同时无法预览布局

    com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: Access from Event Dispatch Thread (EDT) is not allowed; see https://jb.gg/ij-platform-threading for details
    Current thread: Thread[#57,AWT-EventQueue-0,10,main] 254734910 (EventQueue.isDispatchThread()=true)
    SystemEventQueueThread: (same)

问题原因
  • 这个错误是 Android Studio 的一个 Bug,与布局代码本身无关
处理策略
  • 清理项目(Clean Project),即可正常预览布局