为什么我在Android Studio中看不到硬编码文本的任何皮棉错误

时间:2018-10-11 12:40:19

标签: android lint

在阅读Lint工具时,我试图在android studio中的一个简单项目中对其进行测试。刚刚创建了一个空活动的新项目。 mainActivity的xml如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

和Mainactivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

当我尝试运行Analyze >> Inspect Code >>整个项目时,来自android studio;它在检查结果中显示以下内容:inspection result image

是否也应该显示用于TextViews文本的硬编码文本(“ Hello World!”)的警告?

由于未收到警告,因此我尝试在设置>>编辑器>>检查中更改严重性级别,如图in this image所示。但是,检查结果是相同的。没有警告或错误。

我要去哪里错了?

2 个答案:

答案 0 :(得分:1)

找到一个奇怪的原因。如果我将惊叹号从文本(“ Hello World!”)中移出,则会显示错误/警告。由于某种原因,在文本中添加了感叹号后,Lint不会对硬编码文本显示警告。

答案 1 :(得分:0)

发生这种情况是因为字符串Hello World!a special case

if (value == "Hello World!") {
    // This is the default text in new templates. Users are unlikely to
    // leave this in, so let's not add warnings in the editor as their
    // welcome to Android development greeting.
    return
}