Android:Field can be converted to a local varible.的解决办法

Android:Field can be converted to a local varible.的解决办法

前言:

使用 Android Studio 开发 Android 有一段时间了,偶尔会碰到 AS 在一些私有变量上有黄色高亮提示Field can be converted to a local varible,有些强迫症的我还是不希望看到这个黄色的高亮。百度没查到什么有用的信息,还是用谷歌搜到了一些解答。

解析

Field can be converted to a local varible 的完整说明是(纯手打):

This inspection searches for redundant class fields that can be replaced with
 local variables,if all local usages of a field are preceded by assignments to 
that field,the field can be removed and its usages replaced with local variables.

意思是检测到这个变量可以使用局部变量替换,建议删除并写成局部变量。

解决方案

删除private XXX XXX; 这句,并在使用的地方直接声明和实例化。

在 Android Studio for Mac 里可以直接使用快捷键 alt + enter 来快速转换成局部变量。

PS:还是需要英语好点啊,实际上看说明就能明白了,就不用这样通过搜索才明白。

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。