Android 数据绑定:“使用'.'的方法引用已弃用" [英] Android Databinding: "Method references using '.' is deprecated"

查看:25
本文介绍了Android 数据绑定:“使用'.'的方法引用已弃用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中使用数据绑定时,我在编译时收到以下警告:

When using databinding in my app, I get the following warning when compiling:

警告:使用."的方法引用已弃用.而不是 'handler.onItemClick',使用 'handler::onItemClick'

请在下面查看我的 XML.

Please see my XML below.

<?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>
        <variable name="handler" type="ClickHandler"/>
        <variable name="active" type="boolean"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="@{!active ? handler.onItemClick : null}"
        android:background="@color/backgroundWhite"/>
    </RelativeLayout>
</layout>

请注意条件语句中的:

非常简单的消息,直到我更改了."到::".

Pretty straightforward message, until I change the '.' to '::'.

android:onClick="@{!active ? handler::onItemClick : null}"

由于 onItemClick 在条件语句中,它似乎将两个 :: 中的第一个解释为条件的else"语句.在第二个:"上,我收到错误:

Since the onItemClick is inside a conditional statement, it seems to interpret the first of the two ::'s as the 'else' statement of the condition. On the second ':', I get the error:

预期,得到':'

正如@CommonsWare 在评论中建议的那样,将语句反转为"@{active ? null : handler::onItemClick}"也没有帮助,显示了类似的错误(见评论)

As @CommonsWare suggested in the comments, inverting the statement to "@{active ? null : handler::onItemClick}" doesn't help either, a similar error is shown (see comments)

显然,当剥离条件语句时,留下"@{handler::onItemClick}",它仍然给出错误:'!=', '%', '*', '+', ',', '-', '.', '/', <, <<, <=, '==', '>', '>=', '>>', '>>>'或者 '[' expected, got ':' 使用点符号,编译时仍然给出警告

Apparently, when stripping the conditional statement away, being left with "@{handler::onItemClick}", it still gives an error: '!=', '%', '*', '+', ',', '-', '.', '/', <, <<, <=, '==', '>', '>=', '>>', '>>>' or '[' expected, got ':' Using the dot-notation, still gives a warning when compiling

有什么办法可以转义这些::'s,以便正确解释吗?

推荐答案

我的猜测是显示弃用警告是因为 Android 数据绑定当前与 Java 8 不完全兼容.将以下内容放入项目的 build.gradle 文件中应该会隐藏提到的警告.

My guess is that the deprecation warning is shown because Android Data Binding is currently not being fully compatible with Java 8. Putting the following into your project's build.gradle file should hide mentioned warning.

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

除非您在项目中使用 Java 8 功能.

Unless you are using Java 8 features in your project.

这篇关于Android 数据绑定:“使用'.'的方法引用已弃用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆