升级后代码分析错误(意外的命名空间前缀)Android 支持库 23.2.0

Code Analysis Error (Unexpected namespace prefix) after upgrading Android Support Library 23.2.0

我升级到 Android 支持库 23.2.0 并添加了

vectorDrawables.useSupportLibrary = true

到我的 build.gradle,这样我就有了对低于 21 的 API 的矢量可绘制支持。(有关详细信息,请参阅 here)。

我也换了

android:src="@drawable/ic_create_black_24dp"

app:srcCompat="@drawable/ic_create_black_24dp"

在每个使用矢量绘图的 Imageview 中。

该应用编译并运行良好,但代码分析报告:

Error:(56, 9) Unexpected namespace prefix "app" found for tag ImageView

为什么会这样?为什么它在编译时出现错误?

编辑:我添加了

xmlns:app="http://schemas.android.com/apk/res-auto"

在我的根布局中。

需要将此添加到顶部父布局:

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

xmlns:app="schemas.android.com/apk/res-auto" 作为属性添加到您的 ImageViewTop-Level 标签,例如 LinearLayoutCoordinatorLayoutRelativeLayout..等等

<ImageView android:layout_width="match_parent"
           android:layout_height="match_parent"
           app:srcCompat="@drawable/ic_create_black_24dp"
           xmlns:app="http://schemas.android.com/apk/res-auto"/>

或在父布局中

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            xmlns:app="http://schemas.android.com/apk/res-auto"/>

Lint,Android 的代码分析工具,似乎还不知道支持向量可绘制对象。您可以通过将 tools:ignore="MissingPrefix" 添加到 ImageView 标记来安全地忽略该错误。

您看到此错误是因为原始 ImageView 没有 srcCompat 属性。该属性仅由 AppCompatImageView 使用,它被注入而不是您声明的 ImageView。使用重载视图膨胀器时很容易发现此错误。 Lint 执行静态分析,不知道您可以使用 xml 从代码中进行的黑客攻击。

将 XML

中的 ImageView 更改为 android.support.v7.widget.AppCompatImageView