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

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

问题描述

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

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在条件语句中,因此似乎将两个::中的第一个解释为条件的其他"语句.在第二个':'上,我得到了错误:

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:

< expr>预期得到了':'

如@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}" ,它仍然会给出错误:'!=','%','*','+',',','-','.','/',<,<<,< =,'==','>','> =','>>','>>>'或'['预期,得到':'使用点符号,在编译时仍会发出警告

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

是否有任何方法可以逃避这些::,因此可以正确解释?

推荐答案

我的猜测是显示已弃用警告,因为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数据绑定:&amp; quot;使用&amp;#39;的方法引用.&amp;#39;已弃用&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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