抛出IllegalStateException有时会导致应用程序在搜索位置时崩溃(Google地图)

时间:2015-05-22 08:57:52

标签: android google-maps

我有一个地图活动,它使用谷歌地图搜索位置并输出它的地址。我的问题是,它大部分时间都有效,但有时在搜索位置时会崩溃。这是我的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

这是我的代码。有人可以指出为什么会这样吗?

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

1 个答案:

答案 0 :(得分:3)

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

如果getFromLocationName(...)为空,则

IllegalArgumentException会引发locationName,如果网络不可用或发生任何其他I / O问题,则会引发IOException

在LogCat中,我们可以看到getFromLocationName(...)抛出IOException:

  

java.io.IOException:无法解析来自服务器的响应

所以你有它:问题是网络不可用或发生了其他I / O问题。