错误:找不到符号导入com.gourav.news.databinding.ActivityDetailBindingImpl;

时间:2019-02-14 17:49:55

标签: kotlin android-databinding androidx

我尝试构建应用程序,但不断获取/Users/gouravkarwasara/Desktop/News2/app/build/generation/source/kapt/debug/com/gourav/news/DataBinderMapperImpl.java:9:错误:找不到符号 导入com.gourav.news.databinding.ActivityDetailBindingImpl;                                   ^   符号:类ActivityDetailBindingImpl   位置:包com.gourav.news.databinding

此错误。

我尝试清理并构建无效并重新启动

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.gourav.news"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
   implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
   implementation 'androidx.core:core-ktx:1.1.0-alpha04'
   implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'

   implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
   kapt "androidx.lifecycle:lifecycle-compiler:2.0.0"
   implementation "androidx.room:room-runtime:2.0.0"
   kapt "androidx.room:room-compiler:2.0.0"
   implementation 'com.google.android.material:material:1.1.0-alpha03'

   implementation "com.squareup.retrofit2:retrofit:2.4.0"
   implementation "com.squareup.retrofit2:converter-gson:2.4.0"
   implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"

   // Retrofit for networking
   implementation "com.squareup.retrofit2:retrofit:2.4.0"
   implementation "com.squareup.retrofit2:converter-gson:2.4.0"
   implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"

   //Timber for logging
   implementation "com.jakewharton.timber:timber:4.7.1"

   // Image Loading and Caching
   implementation "com.github.bumptech.glide:glide:4.8.0"
   annotationProcessor "com.github.bumptech.glide:compiler:4.8.0"
   implementation "com.google.firebase:firebase-core:16.0.7"
}

activity_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>

<data>

    <import type="com.gourav.news.utils.BindingUtils" />

    <variable
            name="article"
            type="com.gourav.news.model.Article" />
</data>

<ScrollView 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"
            xmlns:bind="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="16dp"
            tools:context=".DetailActivity">

        <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="@id/tv_news_source"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/iv_news_image" />

        <ImageView
                android:id="@+id/iv_news_image"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:contentDescription="@string/image_desc"
                android:foreground="@drawable/news_image_gradient"
                app:layout_constraintDimensionRatio="h,16:9"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                bind:articleUrl="@{article.url}"
                bind:urlToImage="@{article.urlToImage}" />

        <ImageButton
                android:id="@+id/iv_save"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginEnd="4dp"
                android:background="?selectableItemBackgroundBorderless"
                android:contentDescription="@string/image_desc_save_button"
                app:layout_constraintEnd_toStartOf="@id/iv_share"
                app:layout_constraintTop_toTopOf="@id/tv_time"
                app:srcCompat="@drawable/ic_save" />

        <ImageButton
                android:id="@+id/iv_share"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?selectableItemBackgroundBorderless"
                android:contentDescription="@string/image_desc_share_button"
                app:layout_constraintEnd_toStartOf="@id/guideline_right"
                app:layout_constraintTop_toTopOf="@id/tv_time"
                app:srcCompat="@drawable/ic_share" />

        <TextView
                android:id="@+id/tv_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@{BindingUtils.formatDateForDetails(article.publishedAt)}"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="@id/guideline_left"
                app:layout_constraintTop_toBottomOf="@+id/tv_news_title"
                tools:text="@tools:sample/date/ddmmyy" />

        <TextView
                android:id="@+id/tv_news_source"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:paddingBottom="18dp"
                android:text="@{article.source.name}"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="@id/guideline_left"
                app:layout_constraintTop_toBottomOf="@id/tv_time"
                tools:text="The Washington Post" />

        <TextView
                android:id="@+id/tv_news_title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:lineSpacingMultiplier="1.1"
                android:text="@{article.title}"
                android:textColor="@color/colorAccent"
                android:textSize="24sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="@id/guideline_right"
                app:layout_constraintStart_toStartOf="@id/guideline_left"
                app:layout_constraintTop_toBottomOf="@id/iv_news_image"/>

        <TextView
                android:id="@+id/tv_news_desc"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="32dp"
                android:alpha="0.6"
                android:lineSpacingMultiplier="1.3"
                android:text="@{article.description}"
                android:textColor="@color/colorAccent"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="@id/guideline_right"
                app:layout_constraintStart_toStartOf="@id/guideline_left"
                app:layout_constraintTop_toBottomOf="@id/tv_news_source"
                tools:layout_editor_absoluteY="365dp"/>

        <TextView
                android:id="@+id/tv_news_content"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="32dp"
                android:alpha="0.6"
                android:lineSpacingMultiplier="1.3"
                android:text="@{BindingUtils.truncateExtra(article.content)}"
                android:textColor="@color/colorAccent"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="@id/guideline_right"
                app:layout_constraintStart_toStartOf="@id/guideline_left"
                app:layout_constraintTop_toBottomOf="@id/tv_news_desc"/>

        <Button
                android:id="@+id/btn_read_full"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="32dp"
                android:background="@drawable/button_selector"
                android:paddingStart="16dp"
                android:paddingEnd="16dp"
                android:text="@string/button_read_full_story"
                android:textSize="16sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tv_news_content" />

        <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="?actionBarItemBackground"
                android:fitsSystemWindows="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:navigationIcon="@drawable/ic_close" />

        <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="12dp" />

        <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_end="12dp" />

        <androidx.constraintlayout.widget.Barrier
                android:id="@+id/bottom_barrier"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:barrierDirection="bottom"
                app:constraint_referenced_ids="tv_news_desc,tv_news_content,tv_news_title" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>
</layout>

DetailActivity.kt

class DetailActivity : AppCompatActivity() {
    private lateinit var binding: ActivityDetailBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_detail)

    }
}

2 个答案:

答案 0 :(得分:10)

执行以下操作。

  1. 在Android Studio的“构建”标签中,检查哪个gradle任务失败:

enter image description here

  1. 选择Android Studio右上方的“成绩”标签:

enter image description here

  1. 找到失败的gradle任务,然后双击它:

enter image description here

  1. 让gradle任务完成并查看输出。它应该说“ FAILED”,并在其上面的几行告诉您问题出在哪里:

enter image description here

在此示例中,问题是,我为可见性参数使用了布尔值,例如:android:visibility="@{true}"当它应为Integer时,但您的数据绑定错误很可能是其他原因。

答案 1 :(得分:2)

当我在<data>中更改变量名但忘记更改视图时,这发生在我身上。更改后,此错误已解决。