Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:50:47+00:00 2026-05-31T12:50:47+00:00

I am writing a small application using ListView in android environment I wrote the

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:50:48+00:00Added an answer on May 31, 2026 at 12:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on writing a small application using the WPF MediaKit , and I
I've been writing writing a small pygtk application using glade to put together the
I'm working on writing a small WSGI application using Bottle and SqlAlchemy and am
I'm writing a small web application using Perl, HTML::Mason and Apache. I've been using
I am using the Model-View pattern on a small application I'm writing. Here's the
I'm writing a small application using GWT and JDOs to store persistent data. I've
I am writing a small application using Rails 3. In one part of it
I have started learning Python by writing a small application using Python 3.1 and
I am writing a small application about stock monitoring for my company. Using mysql
I'm interested in writing a small Java application using SWT. I'm a blind individual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.