Yohan Blake
Yohan Blake

Reputation: 1318

IllegalStateException thrown sometimes resulting in app crash when searching for location (Google Maps)

I have a map activity which uses Google Maps where it searches for the location and outputs the address of it. My problem is, it works most of the time, but sometimes when searching for the location, it crashes. This is my logcat:

05-22 08:49:03.398: E/AndroidRuntime(4007): FATAL EXCEPTION: main
05-22 08:49:03.398: E/AndroidRuntime(4007): java.lang.IllegalStateException: Could not execute method of the activity
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.view.View$1.onClick(View.java:3591)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.view.View.performClick(View.java:4084)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.view.View$PerformClick.run(View.java:16966)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.os.Handler.handleCallback(Handler.java:615)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.os.Looper.loop(Looper.java:137)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at java.lang.reflect.Method.invoke(Method.java:511)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at dalvik.system.NativeStart.main(Native Method)
05-22 08:49:03.398: E/AndroidRuntime(4007): Caused by: java.lang.reflect.InvocationTargetException
05-22 08:49:03.398: E/AndroidRuntime(4007):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at java.lang.reflect.Method.invoke(Method.java:511)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.view.View$1.onClick(View.java:3586)
05-22 08:49:03.398: E/AndroidRuntime(4007):     ... 11 more
05-22 08:49:03.398: E/AndroidRuntime(4007): Caused by: java.io.IOException: Unable to parse response from server
05-22 08:49:03.398: E/AndroidRuntime(4007):     at android.location.Geocoder.getFromLocationName(Geocoder.java:178)
05-22 08:49:03.398: E/AndroidRuntime(4007):     at com.example.shareity.MapAct.geoLocate(MapAct.java:111)
05-22 08:49:03.398: E/AndroidRuntime(4007):     ... 14 more

This is my code. Can someone please point out why this is happening?

public class Map2 extends FragmentActivity {

    GoogleMap mMap;
    private static final float DEFAULTZOOM = 15;
    public static final int GPS_ERRORDIALOG_REQUEST = 9001;

    //Button btnSelect; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //btnSelect = (Button) findViewById(R.id.btnSelectLoc);

        if(servicesOK()){

            setContentView(R.layout.map2);
            if(initMap()){
                Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();
                //btnSelect = (Button) findViewById(R.id.btnSelectLoc);

                /*btnSelect.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent back = new Intent(getApplicationContext(), EventCreateN.class);
                        startActivity(back);
                    }
                });*/

            }else{
                Toast.makeText(this, "Map not available", Toast.LENGTH_SHORT).show();
            }

        }
        else{
            setContentView(R.layout.map2);
        }
    }

    public boolean servicesOK(){
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if(isAvailable == ConnectionResult.SUCCESS){
            return true;
        }
        else if(GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,this, GPS_ERRORDIALOG_REQUEST);
        }
        else{
            Toast.makeText(this, "Can't connect", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private boolean initMap(){
        if (mMap == null){
            SupportMapFragment mapFrag = 
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mMap = mapFrag.getMap();
        }
            return (mMap != null);
    }

    private void gotoLocation(double lat, double lng, float zoom){
        LatLng ll = new LatLng(lat,lng);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
        mMap.moveCamera(update);
    }

    public void geoLocate(View v) throws IOException{
        hideSoftKeyboard(v);

        EditText et = (EditText) findViewById(R.id.editText1);
        String location = et.getText().toString();

        Geocoder gc = new Geocoder(this);
        List<Address> list = gc.getFromLocationName(location, 1);
        if (list != null && list.size() > 0) {
            Address add = list.get(0);
            //String locality = add.getLocality();
            String add1 = add.getAddressLine(1);
            //String add2 = add.getAddressLine(2);
            //String add3 = add.getAddressLine(3);
            String add0 = add.getAddressLine(0);
            //String add4 = add.getAddressLine(4);

            Toast.makeText(this, add0 + "," + add1, Toast.LENGTH_LONG).show();

            double lat = add.getLatitude();
            double lng = add.getLongitude();

            gotoLocation(lat, lng, DEFAULTZOOM);
            String message = "Current Location \nLongitude: "+lng+"\nLatitude: "+lat;
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            final Intent yourIntent = new Intent(Map2.this, EventCreateN.class);
            Bundle b = new Bundle();

            yourIntent.putExtra("key",add0 + "," + add1);
            yourIntent.putExtra("key2", lng+","+lat);

            //set delay
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {


                    // Do something after 5s = 5000ms

                    startActivity(yourIntent);
                }
            }, 5000);

            /*
            //send double
            b.putDouble("key", lat);
            yourIntent.putExtras(b);*/

        }else{
            Toast.makeText(this, "Location not found", Toast.LENGTH_SHORT).show();
        }
    }

    private void hideSoftKeyboard(View v){
        InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}

Upvotes: 0

Views: 875

Answers (1)

Karim
Karim

Reputation: 5308

According to http://developer.android.com/reference/android/location/Geocoder.html#getFromLocationName(java.lang.String, int):

getFromLocationName(...) throws an IllegalArgumentExceptionif locationName is null and an IOException if the network is unavailable or any other I/O problem occurs.

In your LogCat, we can see that getFromLocationName(...) is throwing an IOException:

java.io.IOException: Unable to parse response from server

So there you have it: the problem is that either the network is unavailable or another I/O problem occurred.

Upvotes: 3

Related Questions