Crash Onlocationchanged() Android
I am getting a crash in onLocationChanged() method and this is what I get in logCat 04-10 00:51:43.354: E/AndroidRuntime(4132): FATAL EXCEPTION: main 04-10 00:51:43.354: E/AndroidR
Solution 1:
gcd.getFromLocation(latitude, longitude, 1);
sometimes returns null. See documentation here.
Try to handle this situation. For example:
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("MyTag", "MyMessage", e); // use this instead e.printStackTrace();
}
if(addresses != null && addresses.size() > 0){
citylb.setText(addresses.get(0).getLocality());
getWeather((String) citylb.getText());
}else{
citylb.setText("No adresses found");
}
Solution 2:
You will have two causes for this exception:
1) your citylb
might not have been initialized.
private TextView citylb;
citylb = (TextView)findViewById(R.id.myTextViewIdinLayout);
2) at this line of code :
citylb.setText(addresses.get(0).getLocality());
addresses
has a null value or is empty!
Solution 3:
Looks like your addresses
is null. Simply move the rest to the try block and catch all Exception
:
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
citylb.setText(addresses.get(0).getLocality());
getWeather((String) citylb.getText());
} catch (Exception e) {
// GeoCoder service not available
e.printStackTrace();
}
Solution 4:
addresses is null, geocoder returns null for unknown locations, you have to test it's result before using it!
Post a Comment for "Crash Onlocationchanged() Android"