当前位置: 首页 > 知识库问答 >
问题:

Android Studio在使用数据绑定时无法生成绑定类

胡墨竹
2023-03-14
  • Android Studio:3.1.0、3.1.1
  • 分级:4.4
  • 生成工具:3.1.0

我正在使用数据绑定库。当我向project添加布局文件或向布局文件中添加任何视图时,Android Studio无法为布局生成绑定类,直到重新构建项目。

布局文件:

<?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 

共有1个答案

葛越
2023-03-14

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

 类似资料:
  • 这过去适用于所有以前版本的JAXB。我已经升级到JAXB的2.2.7版本,现在抛出以下内容: java.lang.AssertionError:javax.xml.bind.jaxBException-带有链接异常:[com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:1 counts of IllegalAnnotat

  • 我正在项目中使用数据绑定,但在xml绑定类中使用和时未生成。 例如,我有activity_main.xml Android Studio:2.1.3 类路径:com.Android.tools.build:Gradle:2.1.3 minSdkVersion 16 targetSdkVersion 24 buildToolsVersion 24.0.0

  • if绑定 if绑定应用在页面元素中,并通过表达式判断是否为元素添加子元素的绑定。if绑定在功能上非常像visible绑定,但在实现上却有很大的不同。visible绑定是为元素添加css样式来控制元素是否显示,if绑定是控制元素的字元素,如果表达式为true,则为元素添加子元素,否则清空子元素。 示例代码: //.W片段 <label> <input type="checkbox" bind-c

  • attr绑定是用来为html元素绑定属性值的,这种绑定非常有用,例如我们需要想一个元素添加title属性,或者为img标签添加src属性。 示例代码: //.W片段 <a bind-attr="{ href: url, title: details }"> Report </a> //js片段 this.url=justep.Bind.observable("year-end.html"),

  • submit绑定只能用在form元素中,当form提交的时候被触发,并且默认阻止form的提交。因此我们通常在submit的处理函数中以ajax的方式提交form表单。 示例代码: //.W片段 <form bind-submit="doSomething"> ... form contents go here ... <button type="submit">Submit</butt

  • with绑定用来创建一个绑定上下文,在子元素内的所有绑定都在这个上下文中进行。 示例代码: //.W片段 <h1 bind-text="city"> </h1> <p bind-with="coords"> Latitude: <span bind-text="latitude"> </span>, Longitude: <span bind-text="longitude"> </span