编译警告:position is a boxed field but needs to be un-boxed to execute
大家可以看下这几个问题:
https://stackoverflow.com/questions/47335090/safeunbox-cannot-be-inverted
https://stackoverflow.com/questions/42872201/data-binding-safeunbox-warning
这日本哥们给了方法:http://blog.shaunkawano.com/entry/2017/11/21/094352
但是好像还解决不了,提示cannot be inverted.
我结合实际,解决了。
1、定义一个类:
package com.xxx.xxx1.common; import android.databinding.InverseMethod; // com.xxx.xxx1.common.DynamicUtil public class DynamicUtil { @InverseMethod("safeUnbox") public static int safeUnbox(java.lang.Integer boxed) { return boxed == null ? 0 : (int)boxed; } public static long safeUnbox(java.lang.Long boxed) { return boxed == null ? 0L : (long)boxed; } public static short safeUnbox(java.lang.Short boxed) { return boxed == null ? 0 : (short)boxed; } public static byte safeUnbox(java.lang.Byte boxed) { return boxed == null ? 0 : (byte)boxed; } public static char safeUnbox(java.lang.Character boxed) { return boxed == null ? '\u0000' : (char)boxed; } public static double safeUnbox(java.lang.Double boxed) { return boxed == null ? 0.0 : (double)boxed; } public static float safeUnbox(java.lang.Float boxed) { return boxed == null ? 0f : (float)boxed; } public static boolean safeUnbox(java.lang.Boolean boxed) { return boxed == null ? false : (boolean)boxed; } }
2、在使用的xml中调用,例如:
android:selection="@{ com.xxx.xxx1.common.DynamicUtil.safeUnbox(Integer.valueOf(myViewModule.mUserInfo.sex)) }"