緯度と経度に基づいて現在の都市と国名を取得する必要がある Android アプリケーションを作成しています。しかし、プログラムは Fatal Error Exception でクラッシュします。次のコードを使用しています。
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
誰でもこの問題で私を助けてもらえますか?
前もって感謝します。