【问题标题】:Fatal Exception while getting current location in android在android中获取当前位置时出现致命异常
【发布时间】:2013-01-29 17:53:50
【问题描述】:

我正在创建一个 android 应用程序,在该应用程序中,我必须根据纬度和经度获取当前城市和国家/地区的名称。但该程序因致命错误异常而崩溃。我正在使用以下代码:

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());
        }
}

这里是 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

谁能帮我解决这个问题?

提前致谢。

【问题讨论】:

    标签: android gps android-location


    【解决方案1】:

    您应该简单地将mAddresses 声明为一个列表:

    List<Address> mAddresses = null;
    

    并删除您在单个 AddressList&lt;Address&gt; 之间来回投射的位置。 getFromLocation() 已经返回 List&lt;Address&gt;,这样您就不会创建 ClassCastExceptions。

    【讨论】:

    • 谢谢。我现在正在获取我的城市和国家/地区名称。
    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多