Android Studio 将 findviewbyid 转换标记为冗余,但它们不是

标签 android android-studio-2.3

所以我有这段代码: 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/

相关文章:

java - 如何以编程方式从菜单中删除 MenuItems?

android-studio-2.3 - 无法在 Android Studio 2.3 中进行发布构建

gradle - 如何修复Android Studio 2.3中的Gradle同步错误?

linux - 如何在 Android Studio 中配置下载文件夹

android-studio - Android Studio 2.3 CMake 不显示错误并且不会编译 C++ 代码

java - 通过用户点击进行碰撞检测

android - Android处理权限已从系统设置中禁用

javascript - 文件传输下载phonegap问题

android - 无法初始化 Git 团队提供程序

Android Studio - 将所有设置保存到帐户?