所以我有这段代码: android studio marks casts as redundant.
但是如果我删除强制转换我会得到编译错误:
Error:(42, 29) Type mismatch: cannot convert from View to Button
Error:Execution failed for task ':app:transformJackWithJackForDebug'. com.android.build.api.transform.TransformException: com.android.builder.core.JackToolchain$ToolchainException: Jack compilation exception
我做错了什么? Android Studio v2.3.3
最佳答案
View#findViewById 在Android <= 25 上的声明是
View findViewById(int resId);
需要显式转换才能使用。
虽然 Android 上的声明 >= 26
<T> T findViewById(int resId);
它为您执行不安全转换并返回赋值的预期值。
如果编译级别为 26,则将使用新方法。
关于Android Studio 将 findviewbyid 转换标记为冗余,但它们不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46813938/