1

I am writing an Android application utilizing the android maps API. I have a Button calling the current location and try to print the address using the Geocoder getFromLocation function.

When I click the Button nothing happens when I use e.printStackTrace() the app bombs out.

Here is the relevant code:

    public void gpsToastButton(View view) {

        Location gps = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);


        if (gps != null) {

            int lat = (int) (gps.getLatitude()  * 1E6);
            int lng = (int) (gps.getLongitude()  * 1E6);

            Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
            try {
                List<Address> myList = myLocation.getFromLocation(lat, lng, 1);
                String message = String
                        .format("gps:" + myList,
                                gps.getLongitude(), gps.getLatitude()
                                );
                Toast.makeText(GoogleMapActivity.this, message, Toast.LENGTH_LONG)
                        .show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                String message2 = String
                        .format("kein anschluss unter dieser nummer");
                Toast.makeText(GoogleMapActivity.this, message2, Toast.LENGTH_LONG);
            }








        }

else{...
    }

and the LogCat after clicking the button:

03-07 05:16:33.318: E/AndroidRuntime(883): FATAL EXCEPTION: main
03-07 05:16:33.318: E/AndroidRuntime(883): java.lang.IllegalStateException: Could not execute method of the activity
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.view.View$1.onClick(View.java:2072)
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.view.View.performClick(View.java:2408)
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.view.View$PerformClick.run(View.java:8816)
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.os.Handler.handleCallback(Handler.java:587)
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.os.Looper.loop(Looper.java:123)
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-07 05:16:33.318: E/AndroidRuntime(883):  at java.lang.reflect.Method.invokeNative(Native Method)
03-07 05:16:33.318: E/AndroidRuntime(883):  at java.lang.reflect.Method.invoke(Method.java:521)
03-07 05:16:33.318: E/AndroidRuntime(883):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-07 05:16:33.318: E/AndroidRuntime(883):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-07 05:16:33.318: E/AndroidRuntime(883):  at dalvik.system.NativeStart.main(Native Method)
03-07 05:16:33.318: E/AndroidRuntime(883): Caused by: java.lang.reflect.InvocationTargetException
03-07 05:16:33.318: E/AndroidRuntime(883):  at google.map.activity.GoogleMapActivity.gpsToastButton(GoogleMapActivity.java:64)
03-07 05:16:33.318: E/AndroidRuntime(883):  at java.lang.reflect.Method.invokeNative(Native Method)
03-07 05:16:33.318: E/AndroidRuntime(883):  at java.lang.reflect.Method.invoke(Method.java:521)
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.view.View$1.onClick(View.java:2067)
03-07 05:16:33.318: E/AndroidRuntime(883):  ... 11 more
03-07 05:16:33.318: E/AndroidRuntime(883): Caused by: java.io.IOException: Service not Available
03-07 05:16:33.318: E/AndroidRuntime(883):  at android.location.Geocoder.getFromLocation(Geocoder.java:117)
03-07 05:16:33.318: E/AndroidRuntime(883):  ... 15 more

Thank you in advance.

//Edit: Ok I solved it now. I throw the IOException to gpsToastButton class and catch it after the last else statement. Thank you for the help!

Yury
  • 20,618
  • 7
  • 58
  • 86
Teonee
  • 107
  • 1
  • 1
  • 8
  • 1
    what do you mean IOException 'does nothing'. – Dan675 Mar 07 '12 at 05:38
  • Normally "java.lang.reflect.InvocationTargetException" occurs when java compiler finds 2 different classes with same name in 2 different packages. when u r importing both classes at a time and when you r trying to create object of that class it throws "java.lang.reflect.InvocationTargetException" exception . – Siva K Mar 07 '12 at 05:40
  • The solution is that when you are creating the object of the class use package name also along with class name so that compiler knows what class it has to use. – Siva K Mar 07 '12 at 05:40
  • 1
    @SivaK That is all complete nonsense. InvocationTargetException occurs when reflectively invoking *any* constructor or method that throws an exception. See the Javadoc. The exception of interest is in the detail member and appears in the stack trace under the 'Caused by' line. It might be a NoClassDefFoundError or ClassNotFoundException having something to do with the scenario you describe, or it might considerably more probably be due to any of an infinite number of other causes. – user207421 Mar 07 '12 at 06:15
  • So could the ' int lat = (int) (gps.getLatitude() * 1E6); ' be the problem? When i use this the application bombs out before the IOException and gives me ' E/AndroidRuntime(1036): Caused by: java.lang.IllegalArgumentException: latitude == 3742200.0 ' in LogCat. Is that what you mean with infinite number? – Teonee Mar 07 '12 at 06:24
  • Ok I solved it now. I throw the IOException to gpsToastButton class and catch it after the last else statement. Thank you for the help! – Teonee Mar 07 '12 at 09:03

1 Answers1

0

The key to this one is the last "Caused by" "Service not Available".

Another answer on SO should help.

It seems like "...any device that doesn't come with Market, GMail etc will also be missing the Geocoder back-end".

Community
  • 1
  • 1
Knossos
  • 15,802
  • 10
  • 54
  • 91
  • the emulator device is running on a Google API ROM and it is also werking on my personal device with CyanogenMod7 so that's a bit awkward. – Teonee Mar 07 '12 at 05:47
  • Which version of the Emulator are you using? Because I believe it's still broken on some versions http://code.google.com/p/android/issues/detail?id=8816 – Mo Kargas Mar 07 '12 at 05:57
  • Sorry, i am farily new to android and java programming. where do i see the Emulator version? The Android version which is emulated has version 2.2-Google – Teonee Mar 07 '12 at 06:21