java.lang.IllegalArgumentException: 我们已经有一个EditText,只能有一个
java.lang.IllegalArgumentException: We already have an EditText, can only have one
错误:
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.access0(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.access0(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>
现在这个错误是从哪里来的?我找到了 "We already have an EditText, can only have one" here,但不知道如何处理。
注意:如果我使用 .xml 添加 EditTexts,我会遇到同样的错误。
以下是我使用的一些来源:
Implement Text Input Layout Using Android Design Support Library
Design Support Library v22.2.0について Part 1
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 用于将每个项目对齐到前一个项目下方。
在你的 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, ...);
错误:
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.access0(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.access0(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>
现在这个错误是从哪里来的?我找到了 "We already have an EditText, can only have one" here,但不知道如何处理。
注意:如果我使用 .xml 添加 EditTexts,我会遇到同样的错误。
以下是我使用的一些来源:
Implement Text Input Layout Using Android Design Support Library
Design Support Library v22.2.0について Part 1
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 用于将每个项目对齐到前一个项目下方。
在你的 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, ...);