java.lang.IllegalArgumentException:我们已经有一个EditText,只能有一个

时间:2015-07-19 15:00:35

标签: android android-design-library

错误:

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>

现在这个错误来自哪里?我发现“我们已经有一个EditText,只能有一个”here,但不知道如何处理它。
注意:如果我使用.xml添加EditTexts,我会得到同样的错误。

以下是我使用的一些来源:
Implement Text Input Layout Using Android Design Support Library
Design Support Library v22.2.0について Part 1

1 个答案:

答案 0 :(得分:6)

TextInputLayout用于处理提示的行为以及如何为EditText显示它。因此,我假设只能向EditText添加一个TextInputLayout

一个简单的解决方案可能是:

在xml中添加更多TextInputLayout并在代码中使用它们,如:

<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用于对齐前一个项目下的每个项目。

在你的班上:

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, ...);