-2

I am trying to create "EditText" dynamically, inside a for loop & I am trying to set an id on every "EditText" so that i can refer to a particular one when taking input.

So far, this is my code-

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

}

App is crashing and my logcat says-

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" is supposed to be the number of EditText created on the screen and this n is passed via intent from another activity which runs fine. Please help me out.

  • If you can, do not save instances of editexts, just save the id of the string (if you just need the id) and then use getResources().getIdentifier("ID_IN_STRING", "id", getPackageName()); Also, in your case I don't think you can simply set id like that – iGoDa Oct 23 '15 at 13:04
  • I suspect this is null `editTexts[i] = (EditText) findViewById(editTexts[i].getId());` because you can't find that view in the activity after you just made it – OneCricketeer Oct 23 '15 at 13:08

2 Answers2

1

Remove, editTexts[i] = (EditText) findViewById(editTexts[i].getId()); As you are creating dynamically EditText you don't need to find those from Activity view.

Means, You don't have those dynamically created EditTexts in your activity xml layout file so Android can not find those EditText from View using findViewById() method.

user370305
  • 103,719
  • 23
  • 157
  • 149
0

Take a look at the answers to this question:

Programmatic Views how to set unique id's?, e.g. the one by user "Xiaochao Yang".

You want to use :

View.generateViewId()

Community
  • 1
  • 1
treesAreEverywhere
  • 3,133
  • 4
  • 22
  • 41