Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: We already have an EditText, can only have one

Error:

07-19 16:39:56.806  29207-29207/ch.android.md E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{ch.swissfm.md/ch.swissfm.md.Login}: java.lang.IllegalArgumentException: We already have an EditText, can only have one
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
        at android.app.ActivityThread.access$600(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5145)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:802)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:569)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: We already have an EditText, can only have one
        at android.support.design.widget.TextInputLayout.setEditText(TextInputLayout.java:150)
        at android.support.design.widget.TextInputLayout.addView(TextInputLayout.java:139)
        at ch.swissfm.md.Login.onCreate(Login.java:46)
        at android.app.Activity.performCreate(Activity.java:5174)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2262)
        at android.app.ActivityThread.access$600(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5145)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:802)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:569)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:802)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:569)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)

Login.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    [..]
    TextInputLayout layout = (TextInputLayout) findViewById(R.id.login_form_layout);
    33 layout.setErrorEnabled(false);
    34 layout.setError(getString(R.string.error));
    35
    36 EditText s2_username = new EditText(this);
    37 EditText s2_pw = new EditText(this);
    38 EditText s2_ip = new EditText(this);
    39 EditText s2_port = new EditText(this);
    40 s2_username.setHint(R.string.s2_username);
    41 s2_pw.setHint(R.string.s2_pw);
    42 s2_ip.setHint(R.string.s2_ip);
    43 s2_port.setHint(R.string.s2_port);
    44 layout.addView(s2_username, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    45 layout.addView(s2_pw, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    46 layout.addView(s2_ip, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    47 layout.addView(s2_port, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    [..]
}

layout_login.xml:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_login">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_form_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_blur"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

Now where is this error coming from? I found "We already have an EditText, can only have one" here, but don't know how to handle it.
Notice: I'm getting the same errror if I use the .xml for adding EditTexts.

Here are some sources I used:
Implement Text Input Layout Using Android Design Support Library
Design Support Library v22.2.0について Part 1

like image 709
Tomblarom Avatar asked Jul 19 '15 15:07

Tomblarom


1 Answers

The TextInputLayout is used to handle the behaviour of the hint an how it is shown for an EditText. So I assume that it is only possible to add one EditText to a TextInputLayout.

A simple solution might be:

Add more TextInputLayout's to your xml and use them in your code like:

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_form_layout_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_form_layout_pw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.design.widget.TextInputLayout>

    (...)

</LinearLayout>

LinearLayout is used to align each item below the previous.

And in your class:

TextInputLayout layout_username = (TextInputLayout) findViewById(R.id.login_form_layout_username);
TextInputLayout layout_pw = (TextInputLayout) findViewById(R.id.login_form_layout_pw);

(...)

layout_username.addView(s2_username, ...);
layout_pw.addView(s2_pw, ...);
like image 162
foxy Avatar answered Nov 03 '22 03:11

foxy