Tiber
Tiber

Reputation: 116

findViewById for child View

I am trying to get items from a child View that is added to the View when a Button is clicked. But when I try to get the elements in the created child View it returns a null.

java.lang.NullPointerException: Attempt to invoke virtual method'android.view.View android.view.View.findViewById(int)' on a null object reference

<Button
    android:id="@+id/buttonAddPerson"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Add Another Person"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView" >

    <LinearLayout
        android:id="@+id/linearLayoutPeople"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></LinearLayout>
</ScrollView>

Layout that is added with button click:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginTop="5dp"
android:id="@+id/linearLayoutPerson">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editTextFirstName"
    android:hint="First Name"
    android:inputType="textPersonName"
    />

 public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

     view = inflater.inflate(R.layout.fragment_create_occupants, container, false);
    Button addPersonButton = (Button)view.findViewById(R.id.buttonAddPerson);


      root = (LinearLayout)view.findViewById(R.id.linearLayoutPeople);

    View child = inflater.inflate(R.layout.list_person_layout,null);
    root.addView(child);

   addPersonButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {

         //  createPersonAdapter.notifyDataSetChanged();
           View child = inflater.inflate(R.layout.list_person_layout,null);
           root.addView(child);

       }
   });

    return view;
}

When I try to get the information:

public void  SaveCall(){

   EditText firstName;

    for (int i =1;i<=root.getChildCount();i++){         
        View child = root.getChildAt(i);
//null at this line            
firstName = (EditText) child.findViewById(R.id.editTextFirstName);

    }
}

Upvotes: 0

Views: 2712

Answers (2)

scubasteve623
scubasteve623

Reputation: 647

As you dynamically create your views, you could add each EditText to an arrayList. Then do your for loop through that list.

Upvotes: 0

Amsheer
Amsheer

Reputation: 7141

Did you try like this

Change this i<=root.getChildCount(); to i<root.getChildCount(); and start from i=0;

 for (int i =0;i<root.getChildCount();i++){         
        View child = root.getChildAt(i);
//null at this line            
firstName = (EditText) child.findViewById(R.id.editTextFirstName);

    }

Upvotes: 2

Related Questions