首页 > 解决方案 > 在布局文件中添加新视图后,它们的 id 没有显示在 DataBinding 变量中,它说无法解析符号

问题描述

我正在使用数据绑定将视图绑定到活动,它工作正常,我能够通过数据绑定对象访问视图,然后我更新了布局文件并添加了更多视图,我再次构建项目但现在我仍然无法获取我添加的新视图,它说无法解析符号。

我尝试再次构建该项目,但没有成功。

我尝试清理项目,然后再次构建它没有工作。

这是 XML 代码:

<?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=".DetailsActivity">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/backdrop_height"
            android:background="@color/textColorPrimaryLight">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginStart="@dimen/dimen_title_text_margin_start"
                app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_collapseMode="parallax">

                    <ImageView
                        android:id="@+id/movieBackdrop_iv"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:contentDescription="@string/image_placeholder_content_description"
                        android:scaleType="centerCrop"
                        app:srcCompat="@drawable/picture_placeholder" />

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/scrim" />
                </FrameLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/app_bar_detail_activity"
                    android:layout_width="match_parent"
                    android:layout_height="?actionBarSize"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:contentInsetStart="@dimen/dimen_inset_start"
                    app:layout_collapseMode="pin"
                    app:navigationIcon="@drawable/arrow_back_white" />

            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>

        <ImageView
            android:elevation="10dp"
            android:contentDescription="@string/image_placeholder_content_description"
            android:id="@+id/moviePoster_iv"
            android:layout_marginTop="160dp"
            android:layout_marginStart="16dp"
            android:layout_width="@dimen/small_poster_width"
            android:layout_height="@dimen/small_poster_height" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:nestedScrollingEnabled="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


            </android.support.constraint.ConstraintLayout>

        </ScrollView>
    </android.support.design.widget.CoordinatorLayout>
</layout>

这是错误

标签: androidandroid-databinding

解决方案


数据绑定字段将与您的 id 不同。id 是在snake_case中给出的,最后你会通过camelCase获得你的 id 字段

例如,如果您设置了 id movieBackdrop_iv,那么您可以使用mBinding.movieBackdropIv.

使用建议而不仅仅是复制粘贴。

更新

我也有像你这样的问题,只需使用 clean、rebuild 或运行项目即可在布局中获取新添加的 id 和变量。


推荐阅读