[kapt]出现了异常:android.databinding.tool.util.LoggedErrorException:发现数据绑定错误。

15

我已经启用了数据绑定,但是在执行代码时,我遇到了这个错误。

错误

e:[kapt]发生异常:android.databinding.tool.util.LoggedErrorException:发现数据绑定错误。

我创建了一个片段类和该类的XML。 我能够导入datbindingutil类。

我尝试了重建/与gradle文件同步/无效缓存并重新启动等操作,但都没有起作用。

XML

<layout>

<!--suppress AndroidUnknownAttribute -->
<data class=".databinding.ProfileFragmentBinding">

    <variable
        name="user"
        type="com.sample.sample.user.User" />

    <variable
        name="vm"
        type="com.sample.sample.user.UserViewModel" />

    <variable
        name="handler"
        type="com.sample.sample.user.profile.ProfileFragment" />
</data>


<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:layout_width="match_parent"
    android:layout_height="match_parent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profileIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/medium"
        android:layout_marginTop="@dimen/medium"
        android:contentDescription="@null"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:url="@{user.avatarUrl}" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="@+id/profileIV"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/profileIV">

        <TextView
            android:id="@+id/profileNameLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/myriad_pro_semibold"
            android:text="@{user.name}"
            android:textColor="@color/black_transparent_de"
            android:textSize="@dimen/text_regular"
            tools:text="NAME" />

        <TextView
            android:id="@+id/badgeLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:fontFamily="@font/myriad_pro_semibold"
            android:text="@{user.badge}"
            android:textColor="@color/grey_000000"
            android:textSize="@dimen/text_regular"
            tools:text="Superman" />

        <TextView
            android:id="@+id/profile_Label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:fontFamily="@font/roboto_bold"
            android:text="@{user.badge}"
            android:textColor="@color/green_39b54a"
            android:textSize="@dimen/text_small"
            tools:text="farmer_v1" />

    </LinearLayout>

    <ImageView
        android:id="@+id/badgeIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/medium"
        android:layout_marginTop="@dimen/medium"
        android:contentDescription="@null"
        android:src="@mipmap/ic_launcher"
        app:error="@{@drawable/ic_profile_default_grey_24dp}"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:placeholder="@{@drawable/ic_profile_default_grey_24dp}"
        app:url="@{user.badgeUrl}" />

    <ImageView
        android:id="@+id/locationPinIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/medium"
        android:contentDescription="@null"
        android:src="@drawable/ic_location_pin"
        app:layout_constraintStart_toStartOf="@+id/profileIV"
        app:layout_constraintTop_toBottomOf="@+id/profileIV" />

    <TextView
        android:id="@+id/profileAddressTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/narrow"
        android:fontFamily="@font/roboto"
        android:textColor="@color/grey_000000"
        app:layout_constraintBottom_toBottomOf="@+id/locationPinIV"
        app:layout_constraintLeft_toRightOf="@+id/locationPinIV"
        app:layout_constraintTop_toTopOf="@+id/locationPinIV"
        tools:text="bangalore, Karnataka" />

    <ImageView
        android:id="@+id/dobIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/standard"
        android:layout_marginTop="@dimen/medium"
        android:contentDescription="@null"
        android:src="@drawable/ic_dob"
        app:layout_constraintLeft_toRightOf="@+id/profileAddressTV"
        app:layout_constraintTop_toBottomOf="@+id/profileIV" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/narrow"
        android:fontFamily="@font/roboto"
        android:textColor="@color/grey_000000"
        app:layout_constraintBottom_toBottomOf="@+id/locationPinIV"
        app:layout_constraintLeft_toRightOf="@+id/dobIV"
        app:layout_constraintTop_toTopOf="@+id/locationPinIV"
        tools:text="born on 01/01/2000" />

    <TextView
        android:id="@+id/activityLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/big"
        android:fontFamily="@font/myriad_pro_semibold"
        android:text="@string/activities"
        android:textColor="@color/black_transparent_de"
        android:textSize="@dimen/text_regular"
        app:layout_constraintStart_toStartOf="@+id/profileIV"
        app:layout_constraintTop_toBottomOf="@+id/locationPinIV" />

    <View
        android:id="@+id/dividerV"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginEnd="@dimen/small"
        android:layout_marginStart="@dimen/small"
        android:layout_marginTop="@dimen/regular"
        android:background="@color/grey_000000"
        app:layout_constraintTop_toBottomOf="@+id/activityLabel" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/dividerV">

        <!--<com.google.android.material.tabs.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:pager="@{(pager)}"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/black"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/black"
            app:tabTextAppearance="@style/CustomTextTab"
            app:tabTextColor="#b4ffffff" />

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tablayout"
            app:handler="@{handler}"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />-->

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

    class ProfileFragment : Fragment() {
    @Inject
    lateinit var mFactory: ViewModelProvider.Factory
    private lateinit var mBinding: ProfileFragmentBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_profile, container, false);
        return mBinding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val vm: UserViewModel = getViewModel(mFactory)
        mBinding.vm = vm
        //mBinding.handler = this
        //mBinding.setLifecycleOwner(this)
    }

    /*@BindingAdapter("bind:handler")
    fun bindViewPagerAdapter(view: ViewPager, activity: MainActivity) {
        val adapter = ProfilePagerAdapter(view.context, activity.supportFragmentManager)
        view.adapter = adapter
    }

    @BindingAdapter("bind:pager")
    fun bindViewPagerTabs(view: TabLayout, pagerView: ViewPager) {
        view.setupWithViewPager(pagerView, true)
    }*/
}

你找到解决这个问题的方法了吗? - thedarkpassenger
从“invalidate cache”中选择只作废,然后关闭项目并从建议中删除项目,重新启动Android Studio,再次打开项目。 - wasim
https://stackoverflow.com/a/63193286/4718406 - Erfan Eghterafi
4个回答

19

在我的情况下,当鼠标悬停在构建输出的那一行时,我能够找到它,如下所示:

未悬停状态: 这里输入图像描述

悬停状态: 这里输入图像描述

真是可惜他们如何显示错误信息,对于最简单的错误,我尝试了10种不同的解决方案,以及清除高速缓存等...

更新:

你也可以点击这里: 这里输入图像描述

然后你会得到像这样的东西:

这里输入图像描述

这是关于错误的非常详细的信息,7年的Android开发中我一直错过了这个按钮:D


1
谢谢!还可以点击错误行,复制它,粘贴到文本编辑器中并在那里检查完整的堆栈跟踪。 - Yamashiro Rion

4
运行./gradlew build --stacktrace以检查详细信息,它会告诉你问题发生的位置,类似于:

e:[kapt]出现异常:android.databinding.tool.util.LoggedErrorException:发现数据绑定错误。 找不到访问器xx 文件:xxx / app / src / main / res / layout / fragment_xxxx.xml 行:108

有时,如果更改了属性名称,特别是在通过refactor => rename更改时,属性名称不会自动更改xml。

2

大多数情况下,当通过数据绑定传递到 XML 文件中的变量名称不正确时,会出现此错误。只需将鼠标悬停在错误上方,您就能够在那里识别出罪魁祸首变量。


0
如果在更改JDK版本时出现问题,请删除项目中每个模块中的所有构建文件夹,这解决了我的问题。
同时也要删除缓存文件夹。
打开Finder -> home -> 按下 "command + shift + ." 查看隐藏文件夹 -> .gradle -> caches。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接