使用DataBinding时,Android Studio无法生成Binding类

时间:2018-04-17 05:39:23

标签: android-studio android-databinding

  • Android Studio:3.1.0,3.1.1
  • Gradle:4.4
  • 构建工具:3.1.0

我正在使用DataBinding库。当我将布局文件添加到项目中或将任何视图添加到布局文件中时,Android Studio无法为布局生成Binding类,直到重建项目为止。

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context=".MainActivity">

    <data>

        <variable
            name="viewModel"
            type="com.qk365.thweeks.MainViewModel" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.TabLayout
            android:id="@+id/main_tab"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:background="@android:color/white"
            android:elevation="5dp"
            app:tabGravity="fill"
            app:tabIndicatorHeight="0dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="#7343ea"
            app:tabTextAppearance="@style/TextAppearance.Design.Tab"
            app:tabTextColor="#181818" />

        <android.support.v4.view.ViewPager
            android:id="@+id/main_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/main_tab">

        </android.support.v4.view.ViewPager>

    </RelativeLayout>
</layout>

我查看了Android Studio日志,当布局文​​件发生变化时,总会出现错误信息。

2018-04-17 11:20:01,699 [e-1024-b02]  ERROR - .intellij.psi.impl.ElementBase - Element: class com.intellij.psi.impl.source.xml.XmlFileImpl because: different providers: SingleRootFileViewProvider{myVirtualFile=file://E:/AndroidStudioProjects/thweeks/module-order/src/main/res/layout/order_fragment_pick_up_order.xml, content=VirtualFileContent{size=2845}}(5200996b); SingleRootFileViewProvider{myVirtualFile=file://E:/AndroidStudioProjects/thweeks/module-order/src/main/res/layout/order_fragment_pick_up_order.xml, content=VirtualFileContent{size=2845}}(398a3993)
invalidated at: see attachment 
com.intellij.psi.PsiInvalidElementAccessException: Element: class com.intellij.psi.impl.source.xml.XmlFileImpl because: different providers: SingleRootFileViewProvider{myVirtualFile=file://E:/AndroidStudioProjects/thweeks/module-order/src/main/res/layout/order_fragment_pick_up_order.xml, content=VirtualFileContent{size=2845}}(5200996b); SingleRootFileViewProvider{myVirtualFile=file://E:/AndroidStudioProjects/thweeks/module-order/src/main/res/layout/order_fragment_pick_up_order.xml, content=VirtualFileContent{size=2845}}(398a3993)
invalidated at: see attachment
    at com.intellij.psi.util.PsiUtilCore.ensureValid(PsiUtilCore.java:502)
    at com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl.createSmartPsiElementPointer(SmartPointerManagerImpl.java:78)
    at com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl.createSmartPsiElementPointer(SmartPointerManagerImpl.java:69)
    at com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl.createSmartPsiElementPointer(SmartPointerManagerImpl.java:64)
    at com.intellij.psi.impl.ElementBase$ElementIconRequest.<init>(ElementBase.java:183)
    at com.intellij.psi.impl.ElementBase$ElementIconRequest.<init>(ElementBase.java:178)
    at com.intellij.psi.impl.ElementBase.computeIcon(ElementBase.java:110)
    at com.intellij.psi.impl.ElementBase.getIcon(ElementBase.java:86)
    at com.intellij.codeInsight.lookup.DefaultLookupItemRenderer._getRawIcon(DefaultLookupItemRenderer.java:79)
    at com.intellij.codeInsight.lookup.DefaultLookupItemRenderer.getRawIcon(DefaultLookupItemRenderer.java:48)
    at com.intellij.codeInsight.completion.JavaMethodCallElement.renderElement(JavaMethodCallElement.java:444)
    at com.intellij.codeInsight.completion.JavaCompletionUtil$4.renderElement(JavaCompletionUtil.java:468)
    at com.intellij.codeInsight.completion.JavaCompletionUtil$4.renderElement(JavaCompletionUtil.java:465)
    at com.intellij.codeInsight.lookup.LookupElementDecorator$VisagisteDecorator.renderElement(LookupElementDecorator.java:179)
    at com.intellij.codeInsight.lookup.LookupElementDecorator.renderElement(LookupElementDecorator.java:83)
    at com.intellij.codeInsight.lookup.impl.LookupCellRenderer.getListCellRendererComponent(LookupCellRenderer.java:150)
    at com.intellij.ui.ExpandedItemListCellRendererWrapper.getListCellRendererComponent(ExpandedItemListCellRendererWrapper.java:38)
    at javax.swing.plaf.basic.BasicListUI.paintCell(BasicListUI.java:209)
    at com.intellij.ui.components.WideSelectionListUI.paintCell(WideSelectionListUI.java:56)
    at javax.swing.plaf.basic.BasicListUI.paintImpl(BasicListUI.java:317)
    at javax.swing.plaf.basic.BasicListUI.paint(BasicListUI.java:240)
    at com.intellij.ui.components.WideSelectionListUI.paint(WideSelectionListUI.java:32)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at javax.swing.JComponent.paintComponent(JComponent.java:780)
    at com.intellij.ui.components.JBList.paintComponent(JBList.java:186)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at com.intellij.ui.components.JBList.paint(JBList.java:118)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JViewport.paint(JViewport.java:728)
    at com.intellij.ui.components.JBViewport.paint(JBViewport.java:236)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:306)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
    at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
    at java.awt.EventQueue.access$500(EventQueue.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:715)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:822)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:650)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:366)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
2018-04-17 11:20:01,699 [e-1024-b02]  ERROR - .intellij.psi.impl.ElementBase - Android Studio 3.1.1  Build #AI-173.4697961 
2018-04-17 11:20:01,699 [e-1024-b02]  ERROR - .intellij.psi.impl.ElementBase - JDK: 1.8.0_152-release 
2018-04-17 11:20:01,699 [e-1024-b02]  ERROR - .intellij.psi.impl.ElementBase - VM: OpenJDK 64-Bit Server VM 
2018-04-17 11:20:01,699 [e-1024-b02]  ERROR - .intellij.psi.impl.ElementBase - Vendor: JetBrains s.r.o 
2018-04-17 11:20:01,699 [e-1024-b02]  ERROR - .intellij.psi.impl.ElementBase - OS: Windows 10 
2018-04-17 11:20:01,699 [e-1024-b02]  ERROR - .intellij.psi.impl.ElementBase - Last Action: EditorChooseLookupItem 

4 个答案:

答案 0 :(得分:6)

我遇到了同样的问题。无论我尝试什么,它都无法生成绑定类。诀窍是你必须重命名你的布局xml文件。假设您有一个xml文件 my_layout.xml ,然后将其重命名为 my_layout_temp.xml 。现在将使用名称 MyLayoutTempBinding 生成绑定类。如果您不喜欢该名称,则可以将其重命名为 my_layout.xml 。最后它将解决绑定类生成问题。

答案 1 :(得分:3)

注意:有关初始调试步骤,请参见 Khemraj 在此处的答案Data Binding class not generated

  • Android Studio:3.1.4
  • 等级:4.4
  • 构建工具:28.0.0

发出

后,我可以立即看到自动生成的绑定类

文件>使缓存无效/重新启动

但仅在索引暂停的情况下,即在Android Studio重新启动后立即进行(我可能应该使用“数据绑定”以外的名称作为测试项目的名称):

enter image description here

enter image description here

在暂停期间,ActivityMain2Binding将被编译器识别,但是 build make 操作不可用。恢复索引后不久,将删除关联的绑定类,并记录OP中提到的IDE错误。

更新(2018.09.11)

使用在上面评论的错误文章(https://issuetracker.google.com/issues/77607040)中找到的提示,将数据绑定设置更改为

enter image description here

然后我添加了对所生成类的引用

enter image description here

答案 2 :(得分:0)

我在与您相同的环境中遇到同样的错误。 但遗憾的是,我无法使用Android Studio 3.1.1解决此问题。 目前,我正在使用 Android Studio 3.2 Canary11,它运行良好

答案 3 :(得分:-2)

这是Android Studio的错误。已在Android Studio 3.2.1中修复。