【问题标题】:Why extends ImageView marked as error?为什么扩展 ImageView 标记为错误?
【发布时间】:2017-03-23 10:59:10
【问题描述】:

Android Studio 将此行标记为错误:

public class ParallaxView extends ImageView

这里是错误:

This custom view should extend android.support.v7.widget.AppCompatImageView instead less... (Ctrl+F1) 

In order to support features such as tinting, the appcompat library will automatically load special appcompat replacements for the builtin widgets.

However, this does not work for your own custom views.  Instead of extending the android.widget classes directly, you should instead extend one of the delegate classes in android.support.v7.widget.AppCompat.

它建议我扩展 AppCompatImageView 但我的 JUnit 测试没有通过,因为 AppCompatImageView 需要一个带有资源的模拟 ContextImageview 不需要这个。

这里的问题解决其他问题:
NullPointerException creating an AppCompatImageView with mock Context

我可以忽略这个错误并使用 ImageView 吗?还有其他解决方案吗?

【问题讨论】:

    标签: android android-view android-support-library android-custom-view android-context


    【解决方案1】:

    使用AppCompat 小部件可以让您在具有预棒棒糖版本 Android 的设备上拥有一些材料设计(和其他新)功能。

    此时AppCompatImageView 仅提供对背景色调和矢量可绘制对象的支持。如果你不使用它们,那么扩展常规的ImageView 就可以了。

    【讨论】:

      【解决方案2】:

      这并不是真正的错误。该应用程序成功构建对吗?只有 Android Studio 将其标记为错误。

      您可以通过编辑“Appcompat 自定义小部件”检查设置将其更改为警告。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 1970-01-01
        • 2022-11-20
        • 1970-01-01
        • 2021-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多