web-dev-qa-db-ja.com

エラーとしてマークされたImageViewを拡張する理由

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を拡張することをお勧めしますが、AppCompatImageViewにはリソースを備えたモックContextが必要であり、Imageviewはこれを必要としないため、JUnitテストに合格しません。

ここで他の問題を解決するための質問:
モックコンテキストでAppCompatImageViewを作成するNullPointerException

このエラーを無視してImageViewを使用できますか?他の解決策はありますか?

11
MarcGV

AppCompatウィジェットを使用すると、Lollipop以前のバージョンのAndroidを搭載したデバイスでマテリアルデザイン(およびその他の新しい)機能を使用できます。

この時点では、AppCompatImageViewは、背景の色合いとベクタードローアブルのみをサポートしています。それらを使用しない場合は、通常のImageViewを拡張することで問題ありません。

18
dev.bmax

これは実際にはエラーではありません。アプリは正常にビルドされますか?エラーとしてマークするのは、Android Studioのみです。

「Appcompat Custom Widgets」検査設定を編集することで、これを警告に変更できます。

3
rickul