“错误:无法生成视图绑定器 java.lang.NullPointerException”的可能原因

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42046732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:19:40  来源:igfitidea点击:

Possible reason for "Error:cannot generate view binders java.lang.NullPointerException"

javaandroiddata-bindingnullpointerexceptionandroid-databinding

提问by atlascoder

I am using Android Studio for my Android projects. I faced an issue when builds crash with strange stacktrace, like this:

我正在将 Android Studio 用于我的 Android 项目。当构建因奇怪的堆栈跟踪而崩溃时,我遇到了一个问题,如下所示:

Error:Execution failed for task ':app:compileDevDebugJavaWithJavac'.
 java.lang.RuntimeException: failure, see logs for details.
  cannot generate view binders java.lang.NullPointerException
    at android.databinding.tool.reflection.ModelMethod.isBoxingConversion(ModelMethod.java:155)
    at android.databinding.tool.store.SetterStore.isBetterParameter(SetterStore.java:946)
    at android.databinding.tool.store.SetterStore.getBestSetter(SetterStore.java:838)

and it was seemed that the databinding became broken as whole.

并且数据绑定似乎整体上被破坏了。

I made refactoring before and moved classes between packages.

我之前进行了重构并在包之间移动了类。

回答by atlascoder

In my case, I relied on Android Studio when renaming and moving classes between packages. But it didn't proceed correction for XMLs of layouts where were references on refactored classes in the typeattribute of variableelement in data.

就我而言,在包之间重命名和移动类时,我依赖于 Android Studio。但它并没有为布局的个XML,其中分别在上重构的类的引用进行校正type的属性variable元素data

So my previous type's value pointed to non existing files and build crashed.

所以我之前类型的值指向不存在的文件并且构建崩溃了。

It's simple mistake but it may take more time to find the source. Hope this would help someone.

这是一个简单的错误,但可能需要更多时间才能找到源头。希望这会帮助某人。

回答by hiddeneyes02

For me, this started happening after updating Android Studio to version 3.5.2

对我来说,这是在将 Android Studio 更新到 3.5.2 版后开始发生的

To fix it, I downgraded Android Gradle Plugin

为了修复它,我降级了 Android Gradle Plugin

buildscript {

    repositories {
        //..........
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        //......
    }
}

回答by Daniel Shatz

Make sure you import all classes you reference in xml bindings

确保导入在 xml 绑定中引用的所有类

I had code something like:

我有类似的代码:

android:visible="@{obj instanceof A}"

I was getting that same error.

我遇到了同样的错误。

Turns out that class Awas not imported on top. Adding <import type="com.company.A">tag resolved the problem.

原来那个类A没有在上面导入。添加<import type="com.company.A">标签解决了问题。

P.S. android:visible is a custom binding adapter I have.

PS android:visible 是我拥有的自定义绑定适配器。

回答by Pouya Danesh

in my case my problem was in a LongClickListenerthat the method for this listener must have returned a booleanbut my method returned voidso when I added the correct return statement it worked just fine.

在我的情况下,我的问题LongClickListener在于这个侦听器的方法必须返回 aboolean但是我的方法返回了void所以当我添加正确的返回语句时它工作得很好。

keep in mind if you use a data binding method you must return the correct value or else the error it throws is not helpful at all.

请记住,如果您使用数据绑定方法,您必须返回正确的值,否则它抛出的错误根本没有帮助。

回答by AdamMc331

You may have stumbled upon this question after migrating to Android X. If that's the case, and you're positive your XML files are okay, but it's still not working, you should start looking at libraries that generate code.

您可能在迁移到 Android X 后偶然发现了这个问题。如果是这样,并且您确定您的 XML 文件没问题,但它仍然无法正常工作,您应该开始查看生成代码的库。

Libraries that generate code cannot be easily converted by the Jettifyer. Some info here - https://blog.danlew.net/2018/11/14/the-reality-of-migrating-to-androidx/

Jettifyer 无法轻松转换生成代码的库。这里的一些信息 - https://blog.danlew.net/2018/11/14/the-reality-of-migrating-to-androidx/

In my case, I updated the obvious ones, but I still was missing something. So, what I did was go into my develop branch (that wasn't Android X), ran a build, and dug into the build folder to see all of the libraries that generated code. Once I did that, I was able to look at the suspects one by one until I found the dependency that was causing this issue. You could either update it, or, in my case, remove it, and this error was resolved. :)

就我而言,我更新了明显的那些,但我仍然遗漏了一些东西。所以,我所做的是进入我的开发分支(不是 Android X),运行构建,然后挖掘构建文件夹以查看生成代码的所有库。一旦我这样做了,我就能够一一查看嫌疑人,直到找到导致此问题的依赖项。您可以更新它,或者,就我而言,将其删除,此错误已解决。:)

回答by senthil kumar

The getter functionfor any instance that you refer from your xml file might not have been defined or the getter function might not have publicaccess specifier.

吸气功能为您从XML文件是指任何情况下可能没有定义或吸气功能可能没有公共访问说明符。

回答by Phan Van Linh

I face this problem when I define variable like this (Android Studio don't warning anything)

当我定义这样的变量时,我遇到了这个问题(Android Studio 不警告任何内容)

<data>
    <variable
        name="onGlobalLayoutA"
        type="ViewTreeObserver.OnGlobalLayoutListener"/>

    <import type="android.view.ViewTreeObserver"/>
</data>

And I solveit by

解决

<data>
    <variable
        name="onGlobalLayoutA"
        type="android.view.ViewTreeObserver.OnGlobalLayoutListener"/>
</data>

Hope this would help someone.

希望这会帮助某人。

回答by zsolt világos

In my case, the reason was that I used a type for a variable which is another module and that module is added as 'implementation ' in build script. Changing to 'api ' resolved the issue.

就我而言,原因是我使用了一个变量类型,该变量是另一个模块,并且该模块在构建脚本中作为“实现”添加。更改为 'api' 解决了该问题。

Clearly not the same issue as the one posted by atlascoder but I mention regardless, maybe someone gits this article with the same problem as I had.

显然与 atlascoder 发布的问题不同,但无论如何我都提到了,也许有人和我遇到了同样的问题。

回答by kotlinski

As @hiddeneyes02 mentioned, this started happen when upgrading from Android Gradle Plugin from 3.5.1 to 3.5.2, this seems to be the bug: https://issuetracker.google.com/issues/143778134

正如@hiddeneyes02 所提到的,这在从 Android Gradle 插件从 3.5.1 升级到 3.5.2 时开始发生,这似乎是错误:https: //issuetracker.google.com/issues/143778134

When I built the project with the command tool I got the same errors as in this post: When building Android project with Android Gradle Plugin v. 3.5.2 my builds fail

当我使用命令工具构建项目时,我遇到了与本文相同的错误: 当使用 Android Gradle 插件 v. 3.5.2 构建 Android 项目时,我的构建失败

回答by Ulrich Beutenmüller

Downgrading Android Gradle Plugin to 3.5.1 fixed the issue for me

将 Android Gradle 插件降级到 3.5.1 为我解决了这个问题