1.关于解决方案参考博客详解全:https://www.jianshu.com/p/c970a4e71395
2.关于Lint的使用详解:https://blog.youkuaiyun.com/u011240877/article/details/54141714
Lint 会根据预先配置的检测标准检查我们 Android 项目的源文件,发现潜在的 bug 或者可以优化的地方,优化的内容主要包括以下几方面:
Correctness:不够完美的编码,比如硬编码、使用过时 API 等
Performance:对性能有影响的编码,比如:静态引用,循环引用等
Internationalization:国际化,直接使用汉字,没有使用资源引用等
Security:不安全的编码,比如在 WebView 中允许使用 JavaScriptInterface 等
---------------------
以下是自己结合博客资料整理的,记录学习内容!
1.警告[Accessibility] Missing contentDescription attribute on image
在定义ImageView这个属性的时候总是会出现 [Accessibility] Missing contentDescription attribute on image 警告,虽说可以不理 但总是感觉怪怪的,在网上一搜 发现原来这是ADT 16.0的新特性,在一些没有文本显示的控件里,如imageView和imageButton等,ADT会提示你定义一个android:contentDescription属性,用来描述这个控件的作用。
解决方法:在imageview属性下加一句:android:contentDescription="@string/desc"警告消失。
2.警告Avoid passing 'null' as the view root(needed to resolve layout parameters on the inflated layout's root element)
问题代码:address_pop = LayoutInflater.from(mContext).inflate(R.layout.contacts_search_popup2, null);
解决方案:如果有父控件可以使用 LayoutInflater. from(mContext). inflate (R.layout.item_activity, parent,false) ;此方法第二个参数为父控件,第三个参数为false加载出来的布局就不会挂在到父控件上,true则反之。如果拿不到父控件也可以使用这个填充器: View.inflate(mContext,R.layout.item_supremacymember_dialog, null);这个与LayoutInflater.from(mContext).inflate(R.layout.item_supremacymember_dialog, null);相同,他(View.inflate)内部还是调用了下面这(LayoutInflater.from(mContext).inflate)。
扩展总结:
1.inflate(R.layout.item,null,false); 通过源码可知这种形式相当于inflate(R.layout.item, null)
root为空,会导致R.layout.item的根布局失效,如果用在ListView或者RecyclerView的布局中会为达到效果,需要内嵌一个布局,这样就造成了根布局的冗余;
2.inflate(R.layout.item,null,true);
不管true或者false,都要手动添加布局到root上。
3.inflate(R.layout.item,root,false);
item的根布局有效,因为attachToRoot为false,需要手动添加item到root上。
4.inflate(R.layout.item,root,true);
item的根布局有效,因为attachToRoot为true,inflate会自动添加item到root上。
3.警告Consider adding android:layout_marginStart="12dp" to better support right-to-left layouts与 When you define paddingLeft you should probably also define paddingRight for right-to-left symmetry
- 原因:这个警报是因为android加入了新的对从右到左阅读顺序的支持要求程序使用end start来代替left right这个start就是当前手机设置的阅读顺序的开始端 比如我们的阅读习惯就是从左到右 那start就是left 反之就是right 第二个的意思是:当你定义paddingLeft你也应该定义paddingRight从右到左的对称
解决方案:添加相应缺少的属性(或者直接忽略属性)
4.Consider replacing 'android:drawableRight' with 'android:drawableEnd=@mipmap/ic_logo' to better support right-to-left layouts.
问题原因同第三条。
解决方案:根据提示进行替换
android:paddingRight 替换成 android:paddingEnd
android:drawableRight替换成android:drawableEnd
5.String literal in setText can not be translated. Use Android resources instead与Do not concatenate text displayed with setText. Use resource string with placeholders
- 解释:就是告诉你setText里面字符串要使用资源文件里面的,第二个的大概意思setText中不要使用拼接的方式
解决方案:将文字替换成:getString(R.string.attentionTa),第二个将拼接的文本提取出来在传进去或者数字使用:String.valueOf()
6.This Handler class should be static or leaks might occur (anonymous android.os.Handler)
- 原因:Handler 类应该应该为static类型,否则有可能造成泄露。在程序消息队列中排队的消息保持了对目标Handler类的应用。如果Handler是个内部类,那 么它也会保持它所在的外部类的引用。为了避免泄露这个外部类,应该将Handler声明为static嵌套类,并且使用对外部类的弱应用
解决方案:给Handler加上static属性,如果你需要在Handler中调用外部Activity的方法,就让Handler持有一个对Activity的WeakReference,这样就不会造成Activity的泄漏。
在java代码中同样可以忽略(ignore) Lint 警告:@SuppressLint(“忽略的警告名称”),如:Handler泄漏(@SuppressLint(“HandlerLeak”))要是你不清楚要忽略的警告具体是什么名字,那就直接忽略 all,当然是当前类/方法/对象:
@SuppressLint("all")
示例代码:
(参考文章:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1106/1922.html)
7.Set android:baselineAligned="false" on this element for better performance
- 原因:如果LinearLayout被用于嵌套的layout空间计算,它的android:baselineAligned属性应该设置成false,以加速layout计算
解决方案:添加:android:baselineAligned="false"
8.possible overdraw:root element paints background @color/a9a9a9_3f with a theme that also paints a background(inferred theme is @style/apptheme)
- 原因:部分区域过度绘制,如:布局的background的颜色与theme的background重复了
解决方案:我没改,因为改了之后好像布局有变动...
(参考文章:http://www.jianshu.com/p/145fc61011cd)
9.Field can be converted to a local variable
解释:该字段可以转换为局部变量
解决:改成局部变量即可
10.问题Deprecated API usage
代码:
问题:使用了过期的api
11.declaration can have final modifier
解释:声明可以有最终修饰符
问题代码:
解决:用final修饰
扩展:
记住,用在字段前,只是它不能被重新赋值,不代表它不能改变本身的值。
比如你的说的final List list = new ArrayList();这个list的任何行为(即方法)都是允许调用的。eg:list.add();
12.拼写错误,可以忽略
13.Lint整体结构预览