Navaneeth
Navaneeth

Reputation: 2584

NullPointerException android ListView

I am writing a small application using ListView in android environment
I wrote the main layoutfile in the following manner

     <?xml version="1.0" encoding="utf-8"?>

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="1dp" android:orientation="vertical">

  <ListView android:id="@+id/lv1" android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="1"/>

 <RelativeLayout android:id="@+id/ll5" android:background="#C5C1AA" android:layout_width="fill_parent" android:layout_height="wrap_content">
 <Button android:paddingLeft="30dp" android:paddingRight="30dp" android:id="@+id/bt1" android:text="save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:onClick="onclick"/>
 <Button android:paddingLeft="30dp" android:paddingRight="30dp" android:id="@+id/bt2" android:text="cancel" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:layout_marginTop="10dp" android:onClick="onclick2"/>
</RelativeLayout>

 </LinearLayout> 


I wrote the adapter class and implemented the getView() method in the following manner

@Override
public View getView(int position, View convertView, ViewGroup parent) {
switch (position) {
case 0:
    convertView = inflater.inflate(R.layout.list1, null);
    break;
case 1:
    convertView=inflater.inflate(R.layout.list2,null);
    break;
default:
    break;
}

return convertView;
}

I Wrote the layoutfiles in the following manner

list1.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <TextView android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Alarm name:"
        android:textSize="15dp"
        android:id="@+id/tv3"
        android:layout_margin="5dp"/>
    <EditText android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/tv3"
        android:id="@+id/et1"
        android:singleLine="true"
        android:layout_margin="5dp"/>

    </RelativeLayout>

list2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <TextView android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_margin="5dp"
        android:text="Password:"
        android:textSize="15dp"
        android:id="@+id/tv4"/>

    <EditText android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/tv4"
        android:id="@+id/et2"
        android:layout_margin="5dp"
        android:singleLine="true"/>

      </RelativeLayout>

I wrote the onclick method in the following manner...

 public void onclick(View v)
 {
     try{
         EditText et=(EditText)findViewById(R.id.et1);
         String s1=et.getText().toString();
         if(s1.equals(""))s1="no name";
         Toast.makeText(getApplicationContext(), s1,Toast.LENGTH_SHORT).show();
         EditText et2=(EditText)findViewById(R.id.et2);
        String s2=et2.getText().toString();
         if(s2.equals(""))s2="no password";
         Toast.makeText(getApplicationContext(), s2,Toast.LENGTH_SHORT).show();
      }
     catch(Exception e)
     {
         Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();
     }

 }

NullPointerException is occurring in onclick() method when accessing et2 and there was no problem in accessing et1
please help me! thanks in advance :)

Upvotes: 1

Views: 165

Answers (3)

Pedro Teran
Pedro Teran

Reputation: 1200

First add a second ListView to you're main.xml

 <?xml version="1.0" encoding="utf-8"?>

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="1dp" android:orientation="vertical">

  <ListView android:id="@+id/lv1" android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="1"/>
<ListView android:id="@+id/lv2" android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="1"/>
     <RelativeLayout android:id="@+id/ll5" android:background="#C5C1AA" android:layout_width="fill_parent" android:layout_height="wrap_content">
     <Button android:paddingLeft="30dp" android:paddingRight="30dp" android:id="@+id/bt1" android:text="save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:onClick="onclick"/>
     <Button android:paddingLeft="30dp" android:paddingRight="30dp" android:id="@+id/bt2" android:text="cancel" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:layout_marginTop="10dp" android:onClick="onclick2"/>
    </RelativeLayout>  
     </LinearLayout> 

Second, tou need to inflate each listView so on your getView method you need to ensure that you're calling the getView class two times(one for each listView)

Upvotes: 0

Mimminito
Mimminito

Reputation: 2843

If you click on the first row, which uses the first layout, the et2 does not exist, and will throw a NullPointerException.

I would advise you name your EditText's the same, which will allow you to access them whichever row you click

Upvotes: 0

MByD
MByD

Reputation: 137312

You are inflating only one layout, but in onClick() you refer to fields from both:

EditText et=(EditText)findViewById(R.id.et);
//                       first one ^
String s1=et.getText().toString();
if(s1.equals(""))s1="no name";
Toast.makeText(getApplicationContext(), s1,Toast.LENGTH_SHORT).show();
EditText et2=(EditText)findViewById(R.id.et2);
//                        second one ^

You need to have a condition which one is used, and refer only to one of them.

Upvotes: 2

Related Questions