web-dev-qa-db-fra.com

La liaison de vue ne fonctionne pas avec "Inclure": types incompatibles: ProgressBarLayoutBinding ne peut pas être converti en ViewDataBinding

Android Studio 3.6. Canaries 12

build.gradle:

buildscript {
    ext.kotlin_version = '1.3.50'
    ext.RETROFIT_VERSION = '2.6.0'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.Android.tools.build:gradle:3.6.0-alpha12'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

dans app/build.gradle:

apply plugin: 'com.Android.application'
apply plugin: 'kotlin-Android'
apply plugin: 'kotlin-Android-extensions'
apply plugin: "kotlin-kapt"

Android {
    viewBinding {
        enabled = true
    }
    dataBinding {
        enabled = true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    compileSdkVersion 29
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
        applicationId "com.Android.testproject.Android.kotlin"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

dans la mise en page 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">

    <data>
        <import type="Android.view.View" />

        <variable
            name="handler"
            type="com.Android.testproject.Android.kotlin.coroutine_retrofit.ui.activity.CoroutinesRetrofitActivity" />

    </data>


    <androidx.constraintlayout.widget.ConstraintLayout
        Android:layout_width="match_parent"
        Android:layout_height="match_parent">

        <include
            Android:id="@+id/toolBarContainer"
            layout="@layout/tool_bar"
            Android:title='@{@string/coroutine_retrofit}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.Android.material.button.MaterialButton
            Android:id="@+id/buttonRetry"
            Android:layout_width="wrap_content"
            Android:layout_height="@dimen/min_height"
            Android:visibility="gone"
            Android:text="@string/retry"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />


        <androidx.recyclerview.widget.RecyclerView
            Android:id="@+id/agentsRecyclerView"
            Android:layout_width="0dp"
            Android:layout_height="0dp"
            Android:visibility="@{handler.agentList.size > 0 ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer"
            tools:listitem="@layout/agent_list_item" />

        <TextView
            Android:id="@+id/noActivityTextView"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:gravity="center"
            Android:text="@string/no_agents"
            Android:visibility="@{handler.isVisibleNoItems ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

        <include
            layout="@layout/progress_bar_layout"
            Android:visibility="gone" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

ici progress_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    Android:id="@+id/containerProgressBarLayout"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:background="#4777"
    Android:clickable="true"
    Android:elevation="2dp"
    Android:focusable="true">

    <ProgressBar
        Android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        Android:layout_width="@dimen/min_height"
        Android:layout_height="@dimen/min_height"
        Android:indeterminateTint="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Ici CoroutinesRetrofitActivity.kt

class CoroutinesRetrofitActivity : AppCompatActivity(), AgentListItemAdapter.AdapterListener {
    var agentList = ObservableArrayList<Agent>()
    private lateinit var binding: CoroutinesRetrofitActivityBinding
    private lateinit var coroutinesRetrofitViewModel: CoroutinesRetrofitViewModel
    val isVisibleNoItems = ObservableBoolean()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = CoroutinesRetrofitActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.setHandler(this);

 this.coroutinesRetrofitViewModel.getIsShowProgress()
            .observe(this, object : Observer<Boolean> {
                override fun onChanged(isShowProgress: Boolean?) {
                    findViewById<View>(R.id.containerProgressBarLayout).visibility =
                        if (isShowProgress!!) View.VISIBLE else View.GONE
                }
            });

    }

Mais quand j'essaye de construire, j'obtiens une erreur:

> Task :app:checkDebugDuplicateClasses
> Task :app:javaPreCompileDebug

> Task :app:compileDebugJavaWithJavac
\testProjects\Android\TestProjectAndroidKotlin\app\build\generated\source\kapt\debug\com\Android\testproject\Android\kotlin\databinding\CoroutinesRetrofitActivityBindingImpl.Java:48: error: incompatible types: ProgressBarLayoutBinding cannot be converted to ViewDataBinding
        setContainedBinding(this.mboundView01);

Mais si je retire du xml ceci:

 <include
            layout="@layout/progress_bar_layout"
            Android:visibility="gone" />

alors l'erreur a disparu.

Qu'est-ce qui ne va pas chez progress_bar_layout?

Si j'inclus directement la barre de progression dans une mise en page comme celle-ci:

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

    <data>

        <import type="Android.view.View" />

        <variable
            name="handler"
            type="com.Android.testproject.Android.kotlin.coroutine_retrofit.ui.activity.CoroutinesRetrofitActivity" />

    </data>


    <androidx.constraintlayout.widget.ConstraintLayout
        Android:layout_width="match_parent"
        Android:layout_height="match_parent">

        <include
            Android:id="@+id/toolBarContainer"
            layout="@layout/tool_bar"
            Android:title='@{@string/coroutine_retrofit}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.Android.material.button.MaterialButton
            Android:id="@+id/buttonRetry"
            Android:layout_width="wrap_content"
            Android:layout_height="@dimen/min_height"
            Android:text="@string/retry"
            Android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />


        <androidx.recyclerview.widget.RecyclerView
            Android:id="@+id/agentsRecyclerView"
            Android:layout_width="0dp"
            Android:layout_height="0dp"
            Android:visibility="@{handler.agentList.size > 0 ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer"
            tools:listitem="@layout/agent_list_item" />

        <TextView
            Android:id="@+id/noActivityTextView"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:gravity="center"
            Android:text="@string/no_agents"
            Android:visibility="@{handler.isVisibleNoItems ? View.VISIBLE : View.GONE}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolBarContainer" />

        <ProgressBar
            Android:id="@+id/containerProgressBarLayout"
            style="?android:attr/progressBarStyle"
            Android:layout_width="@dimen/min_height"
            Android:layout_height="@dimen/min_height"
            Android:indeterminateTint="@color/colorPrimary"
            Android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

le problème a disparu.

Pourquoi avec include ne fonctionne pas?

include
            layout="@layout/progress_bar_layout"
            Android:visibility="gone" />
6
a_subscriber

J'ai utilisé a_subsriber answer et cela fonctionne vraiment, mais assurez-vous de mettre les nampespaces xmlns dans la balise de mise en page mais pas dans la balise de vue

<layout xmlns:Android="http://schemas.Android.com/apk/res/Android">
0
Bios90