web-dev-qa-db-ja.com

ビューバインディングが「インクルード」で機能しない:互換性のないタイプ:ProgressBarLayoutBindingをViewDataBindingに変換できません

Android Studio 3.6。カナリア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
    }
}

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

レイアウト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>

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

ここに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
                }
            });

    }

しかし、ビルドしようとするとエラーが発生します。

> 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);

しかし、私がこれをxmlから削除すると、

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

その後、エラーはなくなりました。

progress_bar_layoutの何が問題になっていますか?

このようなレイアウトにプログレスバーを含めるように指示すると:

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

問題はなくなりました。

なぜincludeが機能しないのですか?

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

私はa_subsriber answer を使用しましたが、それは実際に機能しますが、xmlns nampespacesをレイアウトタグに配置し、ビューのタグには配置しないようにしてください

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