android - lint 常见错误


错误编码  错误说明      举例

40    变量未声明 
506    固定的Boolean值     char c=3;
            if(c<300){}
525    缩排格式错误 
527    无法执行到的语句    if(a > B)
            return TRUE;
            else
             return FALSE;
            return FALSE;
529    变量未引用      检查变量未引用的原因
530    使用未初始化的变量 
534    忽略函数返回值 
539    缩排格式错误 
545    对数组变量使用&     char arr[100], *p;
            p=&arr;
603    指针未初始化     void print_str(const char *p);
            …
            char *sz;
            print_str(sz);
605    指针能力增强     void write_str(char *lpsz);
            …
            write_str(“string”);
613    可能使用了空指针 
616    在switch语句中未使用break; 
650    比较数值时,常量的范围超过了 if( ch == 0xFF ) ...
    变量范围 
713    把有符号型数值赋给了无符号型
    数值 
715    变量未引用 
725    Indentation错误 
734    在赋值时发生变量越界   int a, b, c;
            …
            c=a*b;
737    无符号型变/常量和有变量型
    变/常量存在于同一个表达式中。  
744    在switch语句中没有default 
752    本地声明的函数未被使用 
762    函数重复声明 
774    Boolean表达式始终返回真/假  char c;
            if(c < 300)

3.1.  检查级别设置

Android-Lint所要检查的问题以Issue来描述。

Issue9类(Category):

  

Issue以一个文本短语来作为id,对Issue的定制等操作都是基于id

Android lintSeverity来标识该Issue的危害程度:Fatal/ Error / Warning/

Information/IgnoreIssue的忽略操作其实也就是降低它的SeverityIgnore

Eclipse 中可以在菜单windowàpreferenceàlint error checking中设置规则的检级

级别。

3.2  定制Lint检查的规则

对Android-Lint发现的问题,我们需要进一步的处理:是确实存在的我们自己的设计问题,要解决它;对无关紧要或者是我们特别设计的问题,要在Android-Lint中忽略掉

Android-Lint有默认的检查和报错的规则,但是也可以在Eclipse或者命令行下改变这种规则,从而可以定制Lint检查的规则。

常见的提示及其原因和解决

4.1

1) 错误原因:res中作了支持多种语言的strings.xml,但是里面的内容却没有一一对应(或多或少),如上例: 在中文版的strins.xml里有定义sort_letter等字串,在英文版的string.xml里却没有定义。

android 的建议是: If an application has more than onelocale, then all the strings declared in one language should also be translatedin all other languages.

2) 解决办法: 补全缺少翻译的字串

4.2           

1) 错误地方:

2) 提示:

"main_landscreen_filtration_packagename" is nottranslated in zh-rCN"

3) 错误原因:

         此例实际是上例的补充,当有些字串确实只需要一种语言,而不需要翻译成其它语言的,你也不应该不去理会它,处理方法之一是加上合适的属性translatable="false"。

Android的建议是: If the string should not be translated,you can add the attribute translatable="false" on the <string>element

4) 解决后:

4.3

1) 地方:

2)提示:

Replace "..." with ellipsischaracter (…, &#8230;) ?

3)原因:

HTML特殊字符需用编码代替,注意 &#; 这三个符号都不能少。

其它一些常见的特殊字符,“–”需要用“&#8211;”;“—”需要用“&#8212;”

“½”需要用“&#189;”

4)解决后:

4.4

1) 错误地方:

2) 提示: Should use"sp" instead of "dp" for text sizes

3) 错误原因:

         为了适应不同的屏幕分辨率,字体大小的单位应该使用 sp 进行计量

Android的建议: When setting text sizes, you should normallyuse sp, or "scale-independent pixels". This is like the dp unit, butit is also scaled by the user's font size preference

4) 解决后:

4.5

1) 出错地方:

2) 提示: Avoid using "px" as units; use"dp" instead

3) 出错原因:

         1.dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。 
    这里要特别注意dip与屏幕密度有关,而屏幕密度又与具体的硬件有关,硬件设置不正确,有可能导致dip不能正常显示。在屏幕密度为160的显示屏上,1dip=1px,有时候可能你的屏幕分辨率很大如480*800,但是屏幕密度没有正确设置比如说还是160,那么这个时候凡是使用dip的都会显示异常,基本都是显示过小。 
     dip的换算: 
           dip(value)=(int) (px(value)/1.5 + 0.5) 
2. dp: 很简单,和dip是一样的。 
3. px: pixels(像素),不同的设备不同的显示屏显示效果是相同的,这是绝对像素,是多少就永远是多少不会改变。 
4.  sp: scaled pixels(放大像素). 主要用于字体显示bestfor textsize。 

备注: 根据google的推荐,像素统一使用dip,字体统一使用sp  

         Android的建议: Avoid using"px" as units; use "dp" instead

4) 解决后:

     4.6

1) 出错地方:

         <stringname="page_num">第1/5页</string>

         R.drawable.menu_tab_on

2) 提示:

The resource R.drawable.menu_tab_on appears to be unused

          The resourceR.string.page_num appears to be unused

The following unrelated icon fileshave identical contents: bar_selecd.png,local_music_progressbar_indicator.9.png

3) 错误原因

         资源里有图片或者字串,在代码里没有用到,应该删掉以节省空间和编译时间

Android建议: Unused resources make applications largerand slow down builds.

4.7

1) 出错地方:

        

2) 提示:

         Missing contentDescription attribute on image

3) 错误原因:

      这是ADT 16.0的新特性,在一些没有文本显示的控件里,如imageView和imageButton等,ADT会提示你定义一个android:contentDescription属性,用来描述这个控件的作用,设置View的备注说明。
           在界面上不会有效果,自己在程序中控制,可临时放一点字符串数据。

       Android建议: Non-textual widgets like ImageViews andImageButtons should use the contentDescription attribute to specify a textualdescription of the widget such that screen readers and other accessibilitytools can adequately describe the user interface.

4) 解决后:

4.8

1) 出错地方:

2) 出错提示:

         <uses-permission> tag appears after <application>tag

3) 出错原因:

         androidManifest.xml中,uses-permission、uses-sdk等界定应用需要的标签应该置于<application>标签之前,否则有可能会出现一些小的bug(如一些主题得不到正确应用等)。

    Andriod建议: The <application> tag should appearafter the elements which declare which version you need, which features youneed, which libraries you need, and so on.

4) 解决后:

4.9

1) 警告地方: 

2) 警告提示: 

         <uses-sdk> tag should specify a target API level

3) 警告原因: 

         Uses-sdk应该同时具备minSdkVersion和targetSdkVersion属性

Android建议:The manifest should contain a<uses-sdk> element which defines the minimum minimum API Level requiredfor the application to run, as well as the target version (the highest APIlevel you have tested the version for.)

4) 解决后:

4.10

1) 警告地方:

2) 警告提示:

         Permission is only granted to system apps

3) 警告原因:

         该类权限仅有系统级别的应用才可以使用,其它的一般应用在Manifest.xml里面申明是没有效果的

Android建议: Permissions with the protection levelsignature or signatureOrSystem are only granted to system apps. If an app is aregular non-system app, it will never be able to use these permissions.

4.11

1) 出错地方:

        

2) 提示:

         Should explicitly set android:allowBackup to true or false

3) 错误原因:

     adbbackup -apk com.imangi.templerun -f D:\test.ab,该命令不需要解锁 刷ROOT 

所以对应用来说是具有一定安全隐患的,

          从应用数据的安全角度出发,android建议在application标签内明确加上android:allowBackup的属性,来明确你是否允许用户自行备份数据。只有当android:allowBackup="true"后,用户才可以通过adb backup 和 adb restore命令来对设备上应用的数据进行备份和恢复,备份后,应用的所有数据可以被用户查看。

虽然android默认了该属性为true值,但还是提倡在Manifest上明确指定你是否希望自己的应用数据被用户备份、访问和恢复。

4) 解决后:

4.12

1) 警告地方:

 2) 警告提示:

Exported service does not require permission

3) 原因:

         为了安全起见,Exportedservice,应该设置用户权限来让相应的应用才可以绑定和使用该服务,如果不设置,就是任何应用都可以随意绑定和使用该服务。当然,如果你的服务确实

仅供自己用,谢绝他人的话,也可以指定exported=false属性

Android建议: Exportedservices (services which either set exported=true or contain an intent-filterand do not specify exported=false) should define a permission that an entitymust have in order to launch the service or bind to it. Without this, any application can use this service.

4) 解决后:

4.13

1) 出错地方:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="155dp"

   android:layout_height="189dp"

   android:orientation="vertical" >

    <ImageView

       …....

                 />

    <TextView

        ……

/>

</LinearLayout>

2) 提示:This tag and its childrencan be replaced by one <TextView/> and a compound drawable

3) 出错原因:

         当一个Layout里面,只包含简单的ImageView,TextView时,应该换成一个TextView加

图片的方式,这样更有效率

如android建议:ALinearLayout which contains an ImageView and a TextView can be more efficientlyhandled as a compound drawable

4) 解决后:

Xml里

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="155dp"

    android:layout_height="189dp"  />

<TextView

         android:id="@+id/recentAppName"

        ……

/>

</LinearLayout>

代码里

TextView appName= (TextView) v.findViewById(R.id.recentAppName);

appName.setText(appUnit.getAppName());

         Drawable appIcon =appUnit.getAppIcon();

         appIcon.setBounds(0,0, 120, 120);

appName.setCompoundDrawables(null,appIcon, null, null);

4.13

1) 出错地方:

2) 错误提示:

Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath()instead

3) 错误原因:

         Hardcode问题

andriod建议: Your code should not reference the /sdcardpath directly; instead use Environment.getExternalStorageDirectory().getPath()

4) 解决后:

4.14

1) 警告地方:

2) 警告提示:

         Use Integer.valueOf(nValue) instead

3) 原因:

    New Integer(): 每次都会创建一个新的对象,即在内存中开辟新的空间

         Integer.valueof(): 当传入的整数是-128~127之外时,才会去调用一个new Integer()方法去创建一个新的对象,否则会使用静态类IntegerCache中的cache里的对象。基本上平时的程序中,这个范围的整数是最常用的。所以采用valueof方式,会更有效率和节省内存。Long等其它类型也同理。

Andriod的建议:You shouldnot call the constructor for wrapper classes directly, such as`newInteger(42)`. Instead, call the valueOf factory method,such as Integer.valueOf(42). This will typically use less memory because commonintegers such as 0 and 1 will share a single instance.

4) 解救后:

 

4.17

1) 警告地方:

        

2)提示:

Implicitly using the defaultlocale is a common source of bugs: Use toLowerCase(Locale) instead

3) 原因:

         使用字串的大小写转换方法时,应该明确地指定语言环境,而不应该简单地调用toLowerCase()了事(系统默认的语言环境)。

         一个很明显的例子就是: 土耳其语言里,字母 “i”的大写形式是 ”İ”,而不是” I” .如果系统的默认语言是英语的话,直接调用toLowerCase()自然就会转换出错。

4)解决后:

      

 

4.8

1) 错误地方:

2) 提示:

This Handler class should bestatic or leaks might occur (com.tcl.simpletv.myapps.StartActivity.MainHandler)

3) 错误原因:

     在Android中,Handler特别是当它作为内部类,且里面有访问到外部类的成员时,此Handler类应该申明为static类型,否则有可能造成泄露。在程序消息队列中排队的消息保持了对目标Handler类的应用。如果Handler是个内部类,那么它也会保持它所在的外部类的引用。为了避免泄露这个外部类,应该将Handler声明为static嵌套类,访问外部类时时,使用对外部类的弱应用。

         private HandlermMainHandler;

         mMainHandler= new MainHandler(this);

static class MainHandler extends Handler {

                  WeakReference<StartActivity>mActivity;

                   publicMainHandler(StartActivity activity) {

                            mActivity = newWeakReference<StartActivity>(activity);

                            //TODO Auto-generated constructor stub

                   }

                   @Override

                   public voidhandleMessage(Message msg) {

                            StartActivitytheActivity = mActivity.get();

                            switch(msg.what) {

                            caseGET_APP_LIST:

                                     theActivity.getInstalledAppsData();

                                     break;

                            default:

                                     break;

                            }

                   }

         }

4.16

1) 警告地方:

2) 警告提示:

Use newSparseIntArray(...) instead for better performance

3) 原因:

         SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的类,目的是提高效率,其核心是折半查找函数(binarySearch)

         在Android中,当我们需要定义HashMap<Integer,E>hashmap = new HashMap<Integer,E>

时,可以使用如下的方式来取得更好的性能

         SparseArray<E>sparseArray = new SpaseArray<E>()

4) 解决后:

当然,并不是Android Lint发现的所有错误在任何时候都是合理的,你可以根据你的实际情况选择采纳或者忽略。但我强烈建议大家把每个错误里的详细描述都看一下,这是非常好的学习资料,因为这些错误(或者说是建议)可以说是长久以来android开发经验的一个汇总,非常有价值,其价值甚至超过了发现的问题本身。

猜你喜欢

转载自blog.csdn.net/u013209460/article/details/46326289