java.lang.IllegalStateException:无法在logcat中执行活动错误的方法

时间:2015-09-17 19:07:11

标签: android-layout android-fragments android-intent android-activity

我想在地图中找到位置并且我的代码运行正常但突然我遇到了这些错误,之后我的应用程序运行不正常。我的java代码是

MainActivity.java

 public class MainActivity extends FragmentActivity {

        private static final int GPS_ERRORDIALOG_REQUEST = 9001;
        GoogleMap myMap;

        @SuppressWarnings("unused")
        private static final double SEATTLE_LAT = 47.60621,
                SEATTLE_LNG = -122.33207,
                SYDNEY_LAT = -33.867487,
                SYDNEY_LNG = 151.20699,
                NEWYORK_LAT = 40.714353,
                NEWYORK_LNG = -74.005973;

        private static final float DEFAULTZOOM = 15;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (servicesOK()){
                setContentView(R.layout.map_activity);

                if(initMap1()){
                    Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
                    gotoLocation(SEATTLE_LAT, SEATTLE_LNG,DEFAULTZOOM);
                }
                else{
                    Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
                }           

            }
            else{

            setContentView(R.layout.activity_main);
        }
        }

        private boolean initMap() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        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);
                dialog.show();
            }
            else{
                Toast.makeText(this, "Can't connect to Google Play Services", Toast.LENGTH_SHORT).show();
            }
            return false;


        }
        private boolean initMap1() {
            if (myMap == null){
            SupportMapFragment mapFrag =
                    (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            myMap = mapFrag.getMap();
            }
        return (myMap != null);
        }
        private void gotoLocation(double lat, double lng){
            LatLng ll = new LatLng(lat, lng);
            CameraUpdate update =  CameraUpdateFactory.newLatLng(ll);
            myMap.moveCamera(update);
        }

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

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



            Geocoder gc = new Geocoder(this);
            List<Address> list = gc.getFromLocationName(location, 1);
            Address add = list.get(0);
            String locality = add.getLocality();
            Toast.makeText(this, locality, Toast.LENGTH_LONG).show();

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

            gotoLocation(lat, lng, DEFAULTZOOM);
        }

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


        }

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

        } 

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

和logcat是

09-17 23:39:56.336: E/AndroidRuntime(4097): FATAL EXCEPTION: main
09-17 23:39:56.336: E/AndroidRuntime(4097): Process: com.nabia.Try, PID: 4097
09-17 23:39:56.336: E/AndroidRuntime(4097): java.lang.IllegalStateException: Could not execute method of the activity
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.view.View$1.onClick(View.java:4240)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.view.View.performClick(View.java:5184)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.view.View$PerformClick.run(View.java:20910)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.os.Handler.handleCallback(Handler.java:739)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.os.Looper.loop(Looper.java:145)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.app.ActivityThread.main(ActivityThread.java:5942)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at java.lang.reflect.Method.invoke(Native Method)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at java.lang.reflect.Method.invoke(Method.java:372)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
09-17 23:39:56.336: E/AndroidRuntime(4097): Caused by: java.lang.reflect.InvocationTargetException
09-17 23:39:56.336: E/AndroidRuntime(4097):     at java.lang.reflect.Method.invoke(Native Method)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at java.lang.reflect.Method.invoke(Method.java:372)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.view.View$1.onClick(View.java:4235)
09-17 23:39:56.336: E/AndroidRuntime(4097):     ... 10 more
09-17 23:39:56.336: E/AndroidRuntime(4097): Caused by: java.io.IOException: Timed out waiting for response from server
09-17 23:39:56.336: E/AndroidRuntime(4097):     at android.location.Geocoder.getFromLocationName(Geocoder.java:178)
09-17 23:39:56.336: E/AndroidRuntime(4097):     at com.nabia.Try.MainActivity.geoLocate(MainActivity.java:114)
09-17 23:39:56.336: E/AndroidRuntime(4097):     ... 13 more

任何人请告诉我如何解决这些错误。

0 个答案:

没有答案