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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:48:18+00:00 2026-05-22T16:48:18+00:00

Can you help me with that.. application started to crash when I added the

  • 0

Can you help me with that.. application started to crash when I added the geocoder to get the name of the city by using latitude and longitude..

    double latitude, longitude;
    private TextView lala;

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    longitude = location.getLongitude();
    latitude = location.getLatitude();
    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
        }

        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
    };

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
    lala = (TextView) findViewById(R.id.textView1);

    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
    List<Address> myList = null;
    try {
        myList = myLocation.getFromLocation(latitude, longitude, 1);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    lala.setText((CharSequence) myList);
  • 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-22T16:48:19+00:00Added an answer on May 22, 2026 at 4:48 pm

    lm.getLastKnownLocation can return null, and then tou try to getLongitude() you’ll get NullPointerException

    Also lala.setText((CharSequence) myList); can do the same (throw Exception) cause’ myList can be null here (if Geocoder is failed).

    EDIT
    To deal with exception in Geocoder consider the following checklist

    1. Make sure your latitude and longitude are not null
    2. Make sure what -90 <= latitude <= 90 and -180 <= longitude <= 180
    3. Make sure you are having available network connection

    EDIT2
    Improved code

    private TextView lala;
    
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    
    if(location != null) {
        showMyAddress(location);
    }
    
    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            showMyAddress(location);
        }
    
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub
    
        }
    
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub
    
        }
    
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub
    
        }
    };
    
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
    lala = (TextView) findViewById(R.id.textView1);
    
    
    
    // Also declare a private method
    
    private void showMyAddress(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
        List<Address> myList;
        try {
            myList = myLocation.getFromLocation(latitude, longitude, 1);
            if(myList.size() == 1) {
                lala.setText(myList.get(0).toString());             
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey folks, hoping you can help me get started with IronRuby. I have several
Anyone that can help me out will be very much appreciated. I seem to
Are there any algorithms that can help with hierarchical clustering? Google's map-reduce has only
I would like information on algorithms that can help identify commonality and differences between
Is there a c# library that can help to write and indent Javascript code.
I'm looking for a suite of plugins that can help me finally switch over
Where do I find a good CSS editor that can help me use all
I'm looking for any beyond basic resources that can help me to be proficient
We're looking at patterns such as MVP that can help make us seperate UI
I'm looking for a small tutorial that can help me use svg images in

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.