Android Ошибка привязки данных: ошибка компиляции при импорте класса с тем же именем, что и java .lang.Error в файле макета - PullRequest
2 голосов
/ 06 февраля 2020

В моем коде я создал запечатанный класс Resource со статусом Success, Error & Loading, как показано ниже:

package com.sample.githubsample.vo

sealed class Resource<T>(val data: T?, val message: String? = null)

class Success<T>(data: T?) : Resource<T>(data)

class Error<T>(data: T?, message: String?) : Resource<T>(data, message)

class Loading<T>(data: T?) : Resource<T>(data)

Я пытаюсь использовать класс Resource в макете xml для управления видимостью представления , Ниже приведен мой файл макета (trending_fragment. xml):

<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" />
        <import type="com.sample.githubsample.vo.Success" />
        <import type="com.sample.githubsample.vo.Loading" />
        <import type="com.sample.githubsample.vo.Error" />

    </data>

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

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/pull_to_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/repo_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Однако я получаю следующую ошибку:

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data   `  `binding error(s):`
[databinding] {"msg":"Error has already been defined as java.lang.Error but trying to re-define as 
com.sample.githubsample.vo.Error","file":"D:\\KotlinProjects\\GithubSample\\app\\src\\main
\\res\\layout\\trending_fragment.xml","pos":[]}

at android.databinding.tool.processing.Scope.assertNoError(Scope.java:111)
at android.databinding.annotationprocessor.ProcessDataBinding.doProcess(ProcessDataBinding.java:124)
at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:88)
at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt)
at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:147)

Если я переименую свой класс Error в другое имя, которое не не конфликтует с Java классами по умолчанию, значит, он работает правильно.

Я пытался использовать псевдоним типа в привязке данных, но все равно получал ту же ошибку. Есть ли какие-либо ограничения в использовании имен классов в макетах при использовании привязки данных?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...