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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:04:20+00:00 2026-06-02T00:04:20+00:00

I have this View Adapter method for a ListView which is populated from a

  • 0

I have this View Adapter method for a ListView which is populated from a JSON file.

One of the components I wanna display on a TextView of each list item is the distance between the user’s location and each location which is displayed on the ListView.

But the app crashed everytime I run it, I know there’s something wrong with my code starting from //calculate the distance.

Can anybody help me to analyze and fix on the lines where I went wrong probably?

Here’s the sample of the parsed longitude and latitude from the JSON:

 "latitude": 52.5074779785632,
 "longitude": 13.3903813322449,

and here’s the method I’m using:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView == null){
                convertView = LocationsListActivity.this.getLayoutInflater().inflate(R.layout.listitems, null, true);
            }

            VideoLocation vidLocation = videoLocations[position];
            ImageView v = (ImageView)convertView.findViewById(R.id.image);
            String url = vidLocation.documentary_thumbnail_url;
            v.setTag(url);
            loader.DisplayImage(url, ctx, v);
            TextView titleView = (TextView)convertView.findViewById(R.id.txt_title);
            String title = vidLocation.name;
            titleView.setText(title.toUpperCase());
            TextView descView = (TextView)convertView.findViewById(R.id.txt_list_desc);
            String desc = vidLocation.text;
            descView.setText(desc);

            //calculate the distance
            Location newLocation = new Location("User");
            GeoPoint geopoint = new GeoPoint(
                    (int) (newLocation.getLatitude() * 1E6), (int) (newLocation
                            .getLongitude() * 1E6));
            GeoPoint myposition = geopoint;
            Location locationA = new Location("point A");
            Location locationB = new Location("point B");
            locationA.setLatitude(geopoint.getLatitudeE6() / 1E6);
            locationA.setLongitude(geopoint.getLongitudeE6() / 1E6);

            locationB.setLatitude(vidLocation.latitude/ 1E6);
            locationB.setLongitude(vidLocation.longitude / 1E6);

            double distance = locationA.distanceTo(locationB);
            String distances = String.valueOf(distance);
            TextView distanceView = (TextView)convertView.findViewById(R.id.txt_distance);
            System.out.println(distances);
            distanceView.setText(distances+" m");

            return convertView;
        }
  • 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-02T00:04:21+00:00Added an answer on June 2, 2026 at 12:04 am

    When your application crashes and you ask about it on StackOverflow, you should always include the logcat output of the crash details. The answer is almost always right there and very easy to pick out.

    What you’re doing here is using the Location object incorrectly. If you look at the docs for the constructor you’re using, you’ll see that this just returns a default location object and doesn’t contain the devices actual location. Also, the string that you pass into this is not arbitrary, it’s the name of the location provider to be associated with this Location object.

    To get the user’s last known location, get an instance of the LocationManager and call getLastKnownLocation. This also takes a string which corresponds to a location provider that should be used.

    Read the docs on obtaining user location and look into the Criteria object and the LocationManager.getBestProvider method. These are the safest ways to get the best location and not crash in the process. For example, if you request location passing the GPS provider string and the device doesn’t have a GPS or the user has their GPS turned off, your code will crash (I believe you get a null object from getLastKnownLocation in this case). Also make sure to add the appropriate permissions to the manifest.

    http://developer.android.com/guide/topics/location/obtaining-user-location.html

    http://developer.android.com/reference/android/location/Criteria.html

    http://developer.android.com/reference/android/location/LocationManager.html

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

Sidebar

Related Questions

I have found this example Lazy load of images in ListView from Fedor which
I have a listview in which I am getting values from base adapter.what I
I have one custom ListView called AreasListView which I've included in the XML file.
I have a method which refreshes a list view by re-setting the adapter of
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I have this attribute in my view model: [CustomRequired, EmailRegex] [Display(Name = KeepInformedPersonMail, ResourceType
I have this code in a prepareForSegue method // Get destination view UIViewController *viewController
I have this code to display the data retrieved from DB in accounts_view.php :
I have this method in App Delegate that makes a window and content view
I have a listview that's populated by rows that get their data from a

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.