9

Android Studio中标记为错误这一行:为什么将ImageView标记为错误?

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需要一个模拟Context与资源和Imageview不需要此。

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

我可以忽略此错误和使用ImageView的?其他解决方案?

回答

14

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

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

3

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

您可以通过编辑'Appcompat Custom Widgets'检查设置将其更改为警告。

相关问题