我正在创建一个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
有人可以帮我解决这个问题吗?
提前致谢。
答案 0 :(得分:3)
您只需将mAddresses
声明为列表:
List<Address> mAddresses = null;
并移除您在单个Address
和List<Address>
之间来回转换的位置。 getFromLocation()
已经返回List<Address>
,这样就不会创建ClassCastExceptions。