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

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

问题描述

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

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

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

请参阅下面的XML。

<?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>

请注意条件语句: >

Please note the : from the conditional statement

相当简单的消息,直到我将'。'更改为'::'。

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:

< expr>预期,得到':'

编辑:由于@CommonsWare在评论中建议,将该语句反转为
@ {active?null:handler :: onItemClick}
也没有帮助,显示类似的错误(见注释)

EDIT2:显然,当剥离条件语句时,留下@ {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数据绑定与Ja完全不兼容va 8.
将以下内容放入项目的 build.gradle 文件应隐藏所提及的警告。

My guess is that the deprecation warning is shown because of 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天全站免登陆