【トラブル】【Android】レイアウトで表示されるエラー / 警告について

■ はじめに

Android のレイアウトで表示されるエラー / 警告について扱う

目次

【1】「Missing constraints in ConstraintsLayout」が表示される
【2】「android:layout_width="wrap_content"」で警告が表示される
【3】「[I18N] Hardcoded string "XXX", should use @string resource」で警告が表示される

【1】「Missing constraints in ConstraintsLayout」が表示される

エラー内容

Missing constraints in ConstraintsLayout

This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints  The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX). These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.

原因

 * 制約(constraints)が設定されていないから

 解決策

 * 「Infer constraints」ボタン押下し、 レイアウト内のすべてのビューの制約を作成する

 参考資料

https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q13183970363
https://stackoverflow.com/questions/47866062/why-android-studio-show-error-of-missing-constraints-in-constraintlayout

【2】「android:layout_width="wrap_content"」で警告が表示される

「android:layout_width="wrap_content"」で以下の警告が表示されてしまう

 警告内容

Use a layout_width of 0dip instead of wrap_content for better performance

 対策

修正前

android:layout_width="wrap_content" 

修正後

android:layout_width="0dip"

 参考資料

http://d.hatena.ne.jp/Superdry/20110703/1309661794
http://yan-note.blogspot.jp/2012/11/android-use-layoutheight-of-0dip.html

【3】「[I18N] Hardcoded string "XXX", should use @string resource」で警告が表示される

「android:text="XXX"」で以下の警告が表示されてしまう

 警告内容

[I18N] Hardcoded string "XXX", should use @string resource

 対策

* resourceを使用する

手順

[1] [res]-[values]-[strings.xml] を開く
[2] 「strings.xml」に、表示したい文字列を追加する
 (下記「strings.xml」の「★追加★」を参照のこと)
[3] 警告が表示されている箇所に対して、
 手順[2]で追加した文字列を使用する
 (下記「修正前/修正後」を参照のこと)

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- 省略 -->
    <string name="xxx">XXX</string><!-- ★追加★ -->
</resources>

修正前/修正後

修正前

android:text="XXX"

修正後

android:text="@string/xxx"

参考資料

http://d.hatena.ne.jp/hkhumanoid/20090117/1232194093

関連記事

Android でのトラブル その1
https://dk521123.hatenablog.com/entry/2020/07/28/000000
プロジェクト作成時のトラブル
https://dk521123.hatenablog.com/entry/2018/08/31/165700
エミュレータ に関するトラブル
https://dk521123.hatenablog.com/entry/2013/09/20/232756
Realm に関するトラブル
https://dk521123.hatenablog.com/entry/2020/07/24/000000
Android 開発時のトラブル ~ Eclipse編 ~
https://dk521123.hatenablog.com/entry/2013/09/29/121726