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 7857499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:03:43+00:00 2026-06-02T21:03:43+00:00

I have a list view of places in which I get from a database,

  • 0

I have a list view of places in which I get from a database, I made an OnItemLongClickListener on the items, after the long click an alert dialog is activated with some information, in the message body, which I get from the database concerning the item clicked. The dialog has 3 buttons (Call, Map it and View Photo) which are working fine. The dialog works for the first 8 items and then the dialog doesn’t open and the application force closes for the rest of the items I don’t know why!

Can you please help me?

Here is the code in which the list of all Hotels is displayed:

List<Hotel> values = datasource.getAllHotels();
            if (values.isEmpty())
            {
                Toast.makeText(this, "No results found!", Toast.LENGTH_LONG).show();
                Intent i1 = new Intent(hotelSearchList.this, hotelSearch.class);
                startActivity(i1);
            }
            ArrayAdapter<Hotel> adapter = new ArrayAdapter<Hotel>(this,
                    android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);
            ListView lv = getListView();
            lv.setOnItemLongClickListener(this);

And this is the code for the onItemLongClickListener:

public boolean onItemLongClick(final AdapterView<?> parent, final View view, int position,
        long id) {
    // TODO Auto-generated method stub
    String hotelName = (String) ((TextView)parent.getChildAt(position)).getText();
    final List<Hotel> location = datasource.getHotelLocation(hotelName);
    String loc = "Location: " + TextUtils.join(", ", location);
    List<Hotel> address = datasource.getHotelAddress(hotelName);
    String add = "Address: " + TextUtils.join(", ", address);
    List<Hotel> rating = datasource.getHotelRating(hotelName);
    String rat = "Rating: " + TextUtils.join(", ", rating);
    List<Hotel> phone = datasource.getHotelPhone(hotelName);
    final String phoneN = "Phone: " + TextUtils.join(", ", phone);
    List<Hotel> latitude = datasource.getHotelLat(hotelName);
    final String lat = latitude.get(0).toString();
    List<Hotel> longitude = datasource.getHotelLon(hotelName);
    final String lon = longitude.get(0).toString();
    List<Hotel> photo = datasource.getHotelPhoto(hotelName);
    final String pho = photo.get(0).toString();
    //Toast.makeText(hotelSearchList.this, "The hotel clicked is " + hotelName, Toast.LENGTH_LONG).show();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Information About " + hotelName);
    builder.setMessage(loc + "\n" + add + "\n" + rat + "\n" + phoneN + "\n");

    builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneN));
            startActivity(i);
        }
    });
    builder.setNegativeButton("View Photo", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent i = new Intent(hotelSearchList.this, imageV.class);
            i.putExtra("photo", pho);
            startActivity(i);
        }
    });
    builder.setNeutralButton("Map it", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            Intent i = new Intent(hotelSearchList.this, mapActivity.class);
            i.putExtra("latitude", lat);
            i.putExtra("longitude", lon);
            startActivity(i);
        }
    });

    builder.setCancelable(true);
    builder.show();
    return false;
}

Here is the log cat:

04-28 19:47:15.159: E/AndroidRuntime(384): FATAL EXCEPTION: main
04-28 19:47:15.159: E/AndroidRuntime(384): java.lang.NullPointerException
04-28 19:47:15.159: E/AndroidRuntime(384):  at egypt.interfaceAct.touristicASearchList.onItemLongClick(touristicASearchList.java:134)
04-28 19:47:15.159: E/AndroidRuntime(384):  at android.widget.AbsListView.performLongPress(AbsListView.java:1753)
04-28 19:47:15.159: E/AndroidRuntime(384):  at android.widget.AbsListView.access$600(AbsListView.java:72)
04-28 19:47:15.159: E/AndroidRuntime(384):  at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:1711)
04-28 19:47:15.159: E/AndroidRuntime(384):  at android.os.Handler.handleCallback(Handler.java:587)
04-28 19:47:15.159: E/AndroidRuntime(384):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 19:47:15.159: E/AndroidRuntime(384):  at android.os.Looper.loop(Looper.java:123)
04-28 19:47:15.159: E/AndroidRuntime(384):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-28 19:47:15.159: E/AndroidRuntime(384):  at java.lang.reflect.Method.invokeNative(Native Method)
04-28 19:47:15.159: E/AndroidRuntime(384):  at java.lang.reflect.Method.invoke(Method.java:521)
04-28 19:47:15.159: E/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-28 19:47:15.159: E/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-28 19:47:15.159: E/AndroidRuntime(384):  at dalvik.system.NativeStart.main(Native Method)
  • 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-06-02T21:03:44+00:00Added an answer on June 2, 2026 at 9:03 pm

    This is an excellent time to set some breakpoints in onItemLongClick() and use your debugger. It will help you find the null value or at least find the line where it crashes.

    I’m willing to guess that the number of times the dialog opens is irrelevant, I assume your 8th entry that has a dud. If you click the 8th entry first does it still crash? If so, you check your database, does the row for the 8th hotelName have all of the fields that you request or does one (or more) columns have a null value?

    Addition

    When the list has scrolled and you use this line:

    String hotelName = (String) ((TextView)parent.getChildAt(position)).getText();
    

    The position is out of bounds which returns null causing your NullPointerException since the only children of parent are the visible ones, instead try a faster, simpler approach:

    String hotelname = ((TextView) view).getText().toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of places in listview which i got from parsing JSON.
I have a list view which is bound to a DataTable source. The sorting
I have a list view item which has a button and displays properties on
I have list view and its adapter stored as global variables. I am not
In my application, I have list view. Selecting another item in it, triggers an
I have a list view binded to a data source. How can I enable
I have a list view with 10 columns and I want each row to
I have a list view comprised of the following: // Add column headers for
I have a list view with many rows and have it set so that
I have created a specific List which exists out of the following elements to

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.