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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:41:55+00:00 2026-06-18T00:41:55+00:00

I am creating an android application in which i have to get the current

  • 0

I am creating an android application in which i have to get the current city and country name on basis of latitude and longitude. But the program crashes with a Fatal Error Exception. I am using the following code:

public class MainActivity extends Activity {

    LocationManager mlocManager;
    LocationListener mlocListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void getLoc(View v)
    {
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
            loc.getLatitude();
            loc.getLongitude();
            Address mAddresses = null;

            Geocoder gcd = new Geocoder(getApplicationContext(),
                    Locale.getDefault());
            try {
                mAddresses = (Address) gcd.getFromLocation(loc.getLatitude(),
                        loc.getLongitude(), 1);

            } catch (IOException e) {
                e.printStackTrace();

            }

            @SuppressWarnings("unchecked")
            String cityName = (mAddresses != null) ? ((List<Address>) mAddresses).get(0)
                    .getLocality() : TimeZone.getDefault().getID();
            @SuppressWarnings("unchecked")
            String countryName = (mAddresses != null) ? ((List<Address>) mAddresses).get(0)
                    .getCountryName() : Locale.getDefault().getDisplayCountry()
                    .toString();

            TextView tv = (TextView) findViewById(R.id.textView1);
            tv.setText("City Name: "+cityName+ " Country Name: "+countryName);

    //      mCurrentSpeed.setText(loc.getSpeed());
        }
}

Here is the LogCat:

01-29 22:49:12.007: E/AndroidRuntime(12958): FATAL EXCEPTION: main
01-29 22:49:12.007: E/AndroidRuntime(12958): java.lang.ClassCastException: java.util.ArrayList
01-29 22:49:12.007: E/AndroidRuntime(12958):    at com.example.gpstest.MainActivity$MyLocationListener.onLocationChanged(MainActivity.java:56)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at android.os.Looper.loop(Looper.java:123)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at java.lang.reflect.Method.invoke(Method.java:507)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-29 22:49:12.007: E/AndroidRuntime(12958):    at dalvik.system.NativeStart.main(Native Method)
01-29 22:49:19.199: I/Process(12958): Sending signal. PID: 12958 SIG: 9

Can anybody please help me with this problem?

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-06-18T00:41:57+00:00Added an answer on June 18, 2026 at 12:41 am

    You should simply declare mAddresses as a List:

    List<Address> mAddresses = null;
    

    And remove where you cast it back and forth between a single Address and List<Address>. getFromLocation() already returns a List<Address>, this way you aren’t creating ClassCastExceptions.

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

Sidebar

Related Questions

I am creating an android application in which i have a activity which contain
I am creating an Android application which will have some embedded music inside of
I am creating an android application in which I have to insert an image
I am creating one android application which will have API to create a new
I am creating an small application in Android, which points the current location. I
I'm creating an android application which should parse a Json from a file or
I am creating an android application in which I am using table layout in
I am creating an android application using Java. I have a boolean variable called
I have an Android Bluetooth application which manages a couple of remote devices( Capsules
I need an advice. I am creating an android application that have multiple List

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.