使用Kotlin进行数据绑定会导致资源$ NotFoundException

时间:2017-08-31 13:08:31

标签: android kotlin android-resources android-databinding annotation-processing

上下文

我正在使用library利用数据绑定通过其ViewBindingAdapter上定义自定义属性:

@BindingAdapter({"attribute"})
public static void bindAttribute(@NonNull View view, String attribute) 
{...}

这允许我定义库在运行时处理的自定义属性:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:algolia="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        <!-- ... -->
        android:orientation="horizontal">

        <ImageView
            <!-- ... -->
            algolia:attribute='@{"objectID"}'/>
    </LinearLayout>
</layout>

然后将此视图用作子类RecyclerView的ViewHolder的itemLayout
所有这些在我的 Java 应用程序中都可以正常工作。

问题

我开始在Kotlin建立一个类似的应用程序。为了使用数据绑定,我按照documentation's guide添加了以下内容:

// ./app/build.gradle
apply plugin: 'kotlin-kapt'

android {
    dataBinding {
        enabled true
    }
...
}
dependencies {
    kapt "com.android.databinding:compiler:$plugin_version"
...
}
// ./build.gradle
ext.plugin_version = '2.3.3'
dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
... 
}

但是,运行应用并显示RecyclerView会在其onCreateViewHolder中触发异常:

08-31 14:32:00.511 22067-22067/com.algolia.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.algolia.app, PID: 22067
android.content.res.Resources$NotFoundException: Resource ID #0xffffffd6
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2105)
at android.content.res.Resources.getLayout(Resources.java:1119)
at android.view.LayoutInflater.inflate(LayoutInflater.java:424)
at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:116)
at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:88)
at com.algolia.instantsearch.ui.views.Hits$HitsAdapter.onCreateViewHolder(Hits.java:433)
at com.algolia.instantsearch.ui.views.Hits$HitsAdapter.onCreateViewHolder(Hits.java:398)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6367)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5555)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
at android.view.View.measure(View.java:19909)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:872)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:924)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19909)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:691)
at android.view.View.measure(View.java:19909)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
08-31 14:32:00.511 22067-22067/com.algolia.app E/AndroidRuntime:     
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6343)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:6274)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我不明白Kotlin项目中的相同代码如何触发Resources$NotFoundException。此外,资源ID似乎很奇怪:0xffffffd6等于4294967254,它太大而不能成为有效整数。

什么可能导致这样的问题?我错过了在Kotlin应用程序中使用数据绑定BindingAdapter的东西吗?

1 个答案:

答案 0 :(得分:0)

问题是由底层库发送无效的View资源ID引起的。