1

My app dynamically inflates some XML and adds it to an already existing layout that is nested in a scroll view. In each of the layouts that is inflated, I have an EditText that I need to get the text of later. I get a NullPointerException because it cannot find the editText. Anyways, this is my loop that grabs the text of the EditTexts:

BTW the +1000 is so it doesnt interfere with Items that were set using .setId()

for(i=1;i<numOfItems;i++){
               etIndex = (EditText)findViewById(numOfItems+1000);
                listOptions.add(etIndex.getText().toString());
           }

It is returning a NullPointerException because the etIndex is empty when I try to get the text.

Here is the code where I inflate the EditText:

etItem = (EditText)newView.findViewById(R.id.etItem);
                etItem.setId(numOfItems+1000);
                etItem.setHint("List Item " + numOfItems);
4

1 回答 1

1

改变

etIndex = (EditText)findViewById(numOfItems+1000);

etIndex = (EditText)findViewById(i+1000);

最后一个索引是 equals numOfItems - 1,所以当你使用时,numOfItems你只需 getNullPointer而不是EditText.

于 2013-06-28T19:43:34.203 回答