Lint常见的问题及解决方案

通用的解决方案:

在java代码中同样可以忽略(ignore) Lint 警告:@SuppressLint(“忽略的警告名称”),如:Handler泄漏(@SuppressLint(“HandlerLeak”))要是你不清楚要忽略的警告具体是什么名字,那就直接忽略 all,当然是当前类/方法/对象:
@SuppressLint("all")
在XML中:tools:ignore="忽略"

1、[Accessibility] Missing contentDescription attribute on image

image.png
image.png
这个问题一般都出在:Image相关控件如ImageView和ImageButton都会出现这个警告!
  • 原因:这些控件一般都是有图片的,而图片含有多种物体和颜色,但是考虑到我们小伙伴有少数色弱的小伙伴,可能无法分别图片的物体,所以希望我们加上contentDescription这个属性。至于这个属性是做什么的呢?这个属性是对这个控件的图片进行描述的,比如可能Android的一些程序可以用声音告诉使用者这个图片画的是什么,他们读的就是你contentDescription的内容。
解决方案1:如:加上>>>android:contentDescription="@string/pay_vip">>>这个属性就好
解决方案2:(忽略这个提示)
image.png
image.png

你可别告诉我你在一个一个的加!!!
直接右击这个问题外层的包,把所有需要加的属性的控件全部加上


image.png
image.png

2、To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.

image.png
image.png
这个问题一般都出在:new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  • 原因:它会采取一个额外的参数 - 区域设置要使用。 如果你想确保输出为机器可读以一致的方式(总是看起来是一样的,不管用户的实际区域设置的),你可以选择的 Locale.CHINA,如果你不计较手机的可读性,您可以明确地设置为使用Locale.getDefault().
解决方案1:用2参数的构造方法:new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());

3、This transaction should be completed with a commit() call

image.png
image.png
  • 原因:Android Lint只看到您已经将一些片段事务调用链接在一起,所以会出现这个问题。
解决方案1:你可以改成链式写法,也可以直接忽略。(@SuppressLint("CommitTransaction"))

4、Consider using apply() instead; commit writes its data to persistent storage immediately, whereas apply will handle it in the background

image.png
image.png
  • 翻译:考虑使用apply(),而不是提交立即将其数据写入持久存储,而应用在后台会处理它
解决方案1:用apply()替换掉commit()

(apply没有返回值而commit返回boolean表明修改是否提交成功2.apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘3.apply方法不会提示任何失败的提示
apply的效率高一些,如果没有必要确认是否提交成功建议使用apply。)


5、This LinearLayout should use android:layout_height="wrap_content"

image.png
image.png
  • 翻译:这LinearLayout应该使用android:layout_height = " wrap_content”
解决方案1:改成wrap_content即可

6、This <FrameLayout> can be replaced with a <merge> tag

image.png
image.png
这个问题一般都出在:FrameLayout或者当一个布局包含另一个时
  • 理由:<merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化
解决方案:根据提示将布局替换成<merge></merge>

7、This Handler class should be static or leaks might occur (anonymous android.os.Handler)

image.png
image.png
  • 原因:Handler 类应该应该为static类型,否则有可能造成泄露。在程序消息队列中排队的消息保持了对目标Handler类的应用。如果Handler是个内部类,那 么它也会保持它所在的外部类的引用。为了避免泄露这个外部类,应该将Handler声明为static嵌套类,并且使用对外部类的弱应用
解决方案:给Handler加上static属性,如果你需要在Handler中调用外部Activity的方法,就让Handler持有一个对Activity的WeakReference,这样就不会造成Activity的泄漏。
在java代码中同样可以忽略(ignore) Lint 警告:@SuppressLint(“忽略的警告名称”),如:Handler泄漏(@SuppressLint(“HandlerLeak”))要是你不清楚要忽略的警告具体是什么名字,那就直接忽略 all,当然是当前类/方法/对象:
@SuppressLint("all")
示例代码:
image.png
image.png

参考文章:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1106/1922.html


8、Use new SparseIntArray(...) instead for better performance

image.png
image.png
  • 原因:SparseArray(是一个android提供的一个工具类)单纯从字面上来理解,SparseArray指的是稀疏数组(Sparse array) ,所谓稀疏数组就是数组中大部分的内容值都未被使用(或都为零),在数组中仅有少部分的空间使用。因此造成内存空间的浪费,为了节省内存空间,并且不影响数组中原有的内容值,我们可以采用一种压缩的方式来表示稀疏数组的内容
解决方案:根据提示替换成SparseArray
image.png
image.png

参考文章:https://liuzhichao.com/p/832.html
替换示例:

image.png
image.png

9、Use a layout_height of 0dp instead of fill_parent for better performance

image.png
image.png
这个问题一般都出在:外层是LinearLayout布局中
  • 原因:一个线性布局,中间的子布局设置了Android:layout_weight="1.0"属性,会将剩余空间填充满,这时候子布局的android:layout_width="fill_parent"就没有用了,应该改为android:layout_width="0dp"
解决方案:改为android:layout_width="0dp"

10、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:paddingEnd="12dp)

  • 原因:这个警报是因为android加入了新的对从右到左阅读顺序的支持要求程序使用end start来代替left right这个start就是当前手机设置的阅读顺序的开始端 比如我们的阅读习惯就是从左到右 那start就是left 反之就是right 第二个的意思是:当你定义paddingLeft你也应该定义paddingRight从右到左的对称
解决方案:添加相应缺少的属性 (或者直接加忽略属性)

11、Set android:baselineAligned="false" on this element for better performance

  • 原因:如果LinearLayout被用于嵌套的layout空间计算,它的android:baselineAligned属性应该设置成false,以加速layout计算
解决方案:添加:android:baselineAligned="false"

12、This Cursor should be freed up after use with #close()

image.png
image.png
  • 原因:Cursor使用完后记得释放掉
解决方案:添加:cursor.close();

13、Nested weights are bad for performance

image.png
image.png
  • 原因:在布局进行嵌套使用时,父布局与子布局都使用了android:layout_weight,但不是必须使用时,便会出现如题所示的警告信息
解决方案:去掉一个不需要的android:layout_weight属性

14、This tag and its children can be replaced by one <TextView/> and a compound drawable

image.png
image.png
  • 原因:可以直接给TextView加图片,不需要用的ImageView这个控件。可以使用android:drawableLeft.、android:drawableRight等属性指定
解决方案:根据图片的位置添加相应属性

15、possible overdraw:root element paints background @color/a9a9a9_3f with a theme that also paints a background(inferred theme is @style/apptheme)

image.png
image.png
  • 原因:部分区域过度绘制,如:布局的background的颜色与theme的background重复了
解决方案:我没改,因为改了之后好像布局有变动...

(参考文章:http://www.jianshu.com/p/145fc61011cd


16、This LinearLayout layout or its LinearLayout parent is useless; transfer the background attribute to the other view

image.png
image.png
  • 原因:这LinearLayout LinearLayout布局或其父母是无用的,就是你布局可以删除一个没用的效果是一样的。
解决方案:删除一个不需要的布局就好

17、Typo: In word 'xyz'(拼写错误,可以不管)

image.png
image.png

18、android:singleLine is deprecated: Use maxLines="1" instead

image.png
image.png
  • 翻译:弃用android:单行模式:使用maxLines =“1”
解决方案:将报错的改成android:maxLines="1"

错误的:


image.png
image.png

正确的:


image.png
image.png

19、incomplete translation(国际化文字适配)

解决方案1:(如果你不打算适配国际化)直接全部忽略掉
解决方案2:如果你需要国家化你就添加多国语言

打开编辑器:


image.png
image.png

选择适配的语言:


image.png
image.png

设置:


image.png
image.png

20、Must be one of: Context.POWER_SERVICE, Context.WINDOW_SERVICE, Context.LAYOUT_INFLATER_SERVICE, Context.ACCOUNT_SERVICE, Context.ACTIVITY_SERVICE

image.png
image.png

问题实例:


image.png
image.png
解决方案:
image.png
image.png

21、@id/message_name can overlap @id/cancel_button if @id/message_name grows due to localized text expansion

image.png
image.png
  • 原因:此提示一般出现在RelativeLayout中,此提示的大体意思为:你某个布局(一般出在TextView中)可能会重叠另外一个布局。
解决方案1:如果你自己知道TextView长度肯定不会覆盖到另外一个布局那么你就可以直接使用此语句(tools:ignore="ContentDescription")
解决方案2:如果你不确定是否会覆盖,你同样可以使用以下语句控制字长(android:maxEms="6"

android:singleLine="true" android:ellipsize="end" )

解决方案3:如果你不确定是否会覆盖,你还可以直接加上在某某可能被覆盖控件的左边或者右边

22、String literal in setText can not be translated. Use Android resources instead与Do not concatenate text displayed with setText. Use resource string with placeholders

image.png
image.png
  • 解释:就是告诉你setText里面字符串要使用资源文件里面的,第二个的大概意思setText中不要使用拼接的方式
解决方案:将文字替换成:getString(R.string.attentionTa),第二个将拼接的文本提取出来在传进去或者数字使用:String.valueOf()

23、the resource 'R.color.aqua' appears to be unused

image.png
image.png
  • 解释:说这个资源你没有用到
解决方案:直接删除即可

24、Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

image.png
image.png

25、Possible overdraw: Root element paints background @color/fff with a theme that also paints a background (inferred theme is @style/AppTheme)

image.png
image.png
  • 解释:多次绘制区域;


    image.png
    image.png
解决方案:尽量把background部分的颜色放子布局中,或者自定义主题,将背景色设置进主题,在运用主题
解决方案1:在主题中加: <item name="android:windowBackground">@null</item>

然后在其他布局中最外层的布局都加上背景色


26、Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)

image.png
image.png
  • 解释:避免传递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)。


27、Avoid object allocations during draw/layout operations (preallocate and reuse instead)

image.png
image.png
  • 解释:在draw和layout和onMeasure方法中最好不要创建对象,因为这些方法在一次显示过程中会根据父布局的需要不止一次的调用。
解决方案:将其new 方法提取出去。如:
image.png
image.png

28、The view name (@+id/userPhone) suggests this is a phone number, but it does not include 'phone' in the inputType (id defined here)

image.png
image.png
  • 解释:系统判断你这个可能是手机号码,所以建议你将inputType类型设置成"phone"
解决方案:android:inputType="phone"

29、This text field does not specify an inputType or a hint

image.png
image.png
  • 解释:这个文本框不指定一个inputType或一个提示
解决方案:添加一个inputType属性或者添加一个hint属性

30、Avoid using sizes smaller than 12sp: 8sp

image.png
image.png
  • 解释:避免使用尺寸小于12 sp:8 sp,其实就是想告诉你小于12sp真的太小啦,系统建议你改成12sp,因为他觉得12sp已经很小了,不建议比12sp还小
解决方案:根据系统提示将所又小于12sp的都改了。(我选择忽略此问题)

31、Field can be converted to a local variable

image.png
image.png
  • 解释:字段可以转换为一个局部变量
解决方案:将此变量改为局部变量


原文章链接:http://www.jianshu.com/p/c970a4e71395


猜你喜欢

转载自blog.csdn.net/yijiahuan/article/details/78135182