Andrey Markov Andrey Markov - 2 months ago 10
Android Question

Accessing Element inside Listview gives java.lang.NullPointerException

I'm trying loop through

Listview
to get
EditText
values.

This is my code for getting
Textview
values :

View list_v = null;
ListView listView = (ListView) findViewById(R.id.listViewMain);

ArrayList<String> listitems = new ArrayList<String>();
EditText et;

for (int i = 0; i < listView.getCount(); i++) {
list_v = listView.getChildAt(i);

et = (EditText) list_v.findViewById(R.id.editText_a);
listitems.add(et.getText().toString());

}


and my xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>

<TextView
android:id="@+id/code"
android:layout_width="0dp"
android:textColor="@android:color/black"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_weight="1"

android:gravity="center"/>
<TextView
android:id="@+id/kcode_lbl"
android:layout_width="0dp"
android:textColor="@android:color/black"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_weight="1"/>
</LinearLayout>

<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<EditText
android:id="@+id/editText_a"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:ems="10"
android:layout_weight="1" >
</EditText>
<TextView
android:id="@+id/editText_lbl"
android:layout_width="0dp"
android:textColor="@android:color/black"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/date_value"
android:layout_width="0dp"
android:textColor="@android:color/black"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/date_lbl"
android:layout_width="0dp"
android:textColor="@android:color/black"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_weight="1"/>
</LinearLayout>

</LinearLayout>


my main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dp">

<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingBottom="10dp">

<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/code_txt"
android:layout_weight="0.25"
android:digits="0123456789"
android:inputType="number"
android:focusable="true">
<requestFocus />
</AutoCompleteTextView>

</LinearLayout>
<ListView
android:id="@+id/listViewMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
>
</ListView>
</LinearLayout>


Problem is when I run the code it gives
java.lang.NullPointerException
on this line of code:

list_v.findViewById(R.id.editText_a)


Should I iterate through
LinearLayout
too ?

Answer

Please try this

for (int i = 0; i < listView.getChildCount(); i++) {
        list_v = listView.getChildAt(i);

        et = (EditText) list_v.findViewById(R.id.editText_a);
        listitems.add(et.getText().toString());

    }

Replace getCount() with getChildCount()