0
votes

i'm trying to load locations from the contact's list inside the android phone to google maps. whenever the user clicks on the button, it will load all the contacts into google maps and place a marker on each contact's location.

i already figured out how to get the contact's address but i am having trouble just looping and placing the markers.

        for (int i = 0; i < addresslist.size(); i++) {
        String contact = namelist.get(i).toString();

        StringTokenizer tokens = new StringTokenizer(contact, ",");
        String name = tokens.nextToken();
        String street = tokens.nextToken();
        String city = tokens.nextToken();

        try {
            List<Address> a = geoCoder.getFromLocationName(street + ", "
                    + city, 1);
            double lat = a.get(0).getLatitude();
            double lon = a.get(0).getLongitude();

            LatLng location = new LatLng(lat, lon);

            mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            mMap.addMarker(new MarkerOptions().position(location).title(name));
            // mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,15));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

I have name, address and city stored in a list, it separates them into separate variables. it takes the address and city, converts them into longitude and latitude and then places the markers on the map.

I'm having trouble trying to get it work. It just crashes and giving me errors, the try and catch portion works fine by itself, if i hard code the address in. I'm all out of ideas and I spent 2-3 hours trying to figure out why. Am I doing this wrong? or is there a better solution doing this.

edit - here;s my logcat

05-28 03:23:19.575: E/AndroidRuntime(8632): FATAL EXCEPTION: main 05-28 03:23:19.575: E/AndroidRuntime(8632): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app1project/com.example.app1project.ContactsMapActivity}: java.util.NoSuchElementException 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.access$700(ActivityThread.java:139) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.os.Handler.dispatchMessage(Handler.java:99) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.os.Looper.loop(Looper.java:137) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.main(ActivityThread.java:4918) 05-28 03:23:19.575: E/AndroidRuntime(8632): at java.lang.reflect.Method.invokeNative(Native Method) 05-28 03:23:19.575: E/AndroidRuntime(8632): at java.lang.reflect.Method.invoke(Method.java:511) 05-28 03:23:19.575: E/AndroidRuntime(8632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 05-28 03:23:19.575: E/AndroidRuntime(8632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 05-28 03:23:19.575: E/AndroidRuntime(8632): at dalvik.system.NativeStart.main(Native Method) 05-28 03:23:19.575: E/AndroidRuntime(8632): Caused by: java.util.NoSuchElementException 05-28 03:23:19.575: E/AndroidRuntime(8632): at java.util.StringTokenizer.nextToken(StringTokenizer.java:208) 05-28 03:23:19.575: E/AndroidRuntime(8632): at com.example.app1project.ContactsMapActivity.onCreate(ContactsMapActivity.java:79) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.Activity.performCreate(Activity.java:5048) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 05-28 03:23:19.575: E/AndroidRuntime(8632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052) 05-28 03:23:19.575: E/AndroidRuntime(8632): ... 11 more

2
can you post the logcat output of when it crashes?chopchop
the error occurs when parsing the contact String. Try logging those strings to see what they look like. I bet one of them looks weird or is empty and that's what causes the crashchopchop
problem solved. i should really start checking logcat carefully. thanksuser1704756

2 Answers

0
votes
Caused by: java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:208)
at com.example.app1project.ContactsMapActivity.onCreate(ContactsMapActivity.java:79)

says it all. Your problem is at that line. The String contact most likely doesn't have enough commas.

0
votes

Have you tried this?,

    public class MainActivity extends Activity {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

    if (map!=null){
      Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
          .title("Hamburg"));
      Marker kiel = map.addMarker(new MarkerOptions()
          .position(KIEL)
          .title("Kiel")
          .snippet("Kiel is cool")
          .icon(BitmapDescriptorFactory
              .fromResource(R.drawable.ic_launcher)));
    }

  }