このコンポーネントのスタイルでは、アプリのテーマがTheme.MaterialComponents(または子孫)である必要があります


8

私はAndroidを初めて使用するので、ばかげた/ばかげた質問があるかもしれません。複数の入力フィールドを動的に作成するアクティビティがあります。入力フィールドの数はユーザーが定義します。

入力はスタイルが設定され、2つの要素で構成されており、要素が毎回同じである複数のパラメーターを持っているため、これらの要素を毎回作成することを望まなかったためです。そのため、この2つの要素のために、プログラムで入力を作成するために使用するXMLファイルを作成しました。

View_input.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayoutTableName"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="10dp"
        android:hint="@string/create_table_name"
        android:inputType="text"
        app:boxStrokeColor="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:placeholderText="@string/create_table_table_name_placeholder"
        app:startIconTintMode="src_in">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textInputName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:text="" />
    </com.google.android.material.textfield.TextInputLayout>
</merge>

ターゲットXML(activity_create_table_column_names.xml)は次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout         
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.createtable.CreateTableColumnNamesActivity">

<LinearLayout
    android:id="@+id/columnNameInputContainer"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:orientation="vertical"
    android:padding="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/create_table_column_names"
    android:textStyle="bold" />

    <!-- HERE --> 
</LinearLayout>

私が書いた場所では<!-- HERE -->、すべての入力フィールドが必要です。

1つの入力を表示することから始めました。

private fun initLayout() {
    val container = findViewById<LinearLayout>(R.id.columnNameInputContainer)
    val inflater = applicationContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val view = inflater.inflate(R.layout.view_input, LinearLayout(this))
    container.addView(view)
}

しかし、次の例外が発生しました:

Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:115)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:458)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:417)

私のStyles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
        ...
    </style>
</resources>

何が悪いのですか?これは私がこのようなものをプログラムする正しい方法ですか?

編集 Android.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="de.hsos.ma.adhocdb">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:appComponentFactory="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:appComponentFactory">
        <activity
            android:name=".ui.createtable.CreateTableColumnNamesActivity"
            android:parentActivityName=".ui.homescreen.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ui.createtable.CreateTableActivity"
            android:parentActivityName=".ui.homescreen.MainActivity" />
        <activity
            android:name=".ui.homescreen.TestTableActivity"
            android:parentActivityName=".ui.homescreen.MainActivity">

            <!--
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            -->
        </activity>
        <activity
            android:name=".ui.TableShow.TableShowActivity"
            android:parentActivityName=".ui.homescreen.MainActivity" />
        <activity android:name=".ui.tablelist.LayoutTableListItem" />
        <activity android:name=".ui.homescreen.MainActivity">

        </activity>
    </application>

</manifest>

あなたのAndroidManifest.xmlコンテンツを投稿できますか
Mohammed Alaa

@MohammedAlaaはい、投稿の下部にマニフェストを追加しました
Barney Stinson

回答:


1

解決策を見つけ、ちょうどあなたを修正活動ではないアプリケーションからあなたのインフレータを取得しinitLayout()、このように

// here is the fix
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

ありがとうございます!!! @MohammedAlaa複数の入力を作成した場合、さまざまな入力( "@ + id / textInputName")のテキストをどのように読み取ることができるか知っていますか?
Barney Stinson、

これが同じであることを確認してください。回答が役に立った場合も受け入れて、他の人が同じ問題を検索したときに解決策が見つかるようにします
Mohammed Alaa

1
御時間ありがとうございます!ビューの作成中にすべてのテキストビューを配列に追加しただけでうまくいきました!
Barney Stinson、
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.