我正在尝试动态创建"EditText"
,在for循环和&我试图在每个"EditText"
上设置一个id,这样我就可以在输入时引用一个特定的id。
到目前为止,这是我的代码 -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String ss = intent.getStringExtra("input");
int n = Integer.parseInt(ss);
EditText[] editTexts = new EditText[n];
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
for (int i = 0; i < n; i++) {
editTexts[i] = new EditText(this);
editTexts[i].setId(i);
editTexts[i] = (EditText) findViewById(editTexts[i].getId());
linearLayout.addView(editTexts[i]);
}
linearLayout.setPadding(10, 0, 10, 0);
}
应用程序崩溃,我的logcat说 -
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: FATAL EXCEPTION: main
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: Process: com.example.akshatmalviya.fairsharedemo3, PID: 16523
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.akshatmalviya.fairsharedemo3/com.example.akshatmalviya.fairsharedemo3.Main2Activity}: java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.view.ViewGroup.addView(ViewGroup.java:4077)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.view.ViewGroup.addView(ViewGroup.java:4059)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at com.example.akshatmalviya.fairsharedemo3.Main2Activity.onCreate(Main2Activity.java:34)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-23 17:34:01.743 16523-16523/com.example.akshatmalviya.fairsharedemo3 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
"n"
应该是屏幕上创建的EditText
的数量,这个n是通过另一个运行良好的活动的意图传递的。
请帮帮我。
答案 0 :(得分:1)
删除,editTexts[i] = (EditText) findViewById(editTexts[i].getId());
当您动态创建EditText时,您不需要在“活动”视图中查找这些内容。
意味着,您在活动xml布局文件中没有动态创建的EditTexts,因此Android无法使用findViewById()
方法从View中找到这些EditText。
答案 1 :(得分:0)