Dec 10, 2012

f Comment

Fixing Android Google API Error: Map Has Zero Size

Amazon If you use com.google.android.maps.MapView in Android Google APIs (I use API level 16, or Android 4.1) to integrate Google Map into your Android app you may see the following exception or error in LogCat in Eclipse:

E/AndroidRuntime(1253): FATAL EXCEPTION: main
E/AndroidRuntime(1253): java.lang.IllegalStateException: Map has zero size
E/AndroidRuntime(1253): at android_maps_conflict_avoidance.com.google.googlenav.map.Map.drawMap(Map.java:818)
....

This is driving you nuts! What is this error about and how do you fix it?

What caused this error?
You are seeing this error because in your layout you are leaving NO space for Google APIs to display the map in your app. That means you defined your layout such that there's no space left for com.google.android.maps.MapView layout element. There are many ways this can happen and it all depends on how you define your layout.

For example suppose you have a LinearLayout with "android:orientation" set to "vertical". In this LinearLayout you define a ListView and com.google.android.maps.MapView like the following:
...
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <ListView
        android:id="@+id/possibleLocationView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="10dp"
    />

    <com.google.android.maps.MapView
        android:id="@+id/googleMapView"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:apiKey="..."
    />
</LinearLayout>
...
This layout would cause the aforementioned fatal exception when you run the app. Read on to see how to fix it!

If you use Eclipse to generate a default Android application your main layout file should be located at [your workspace]\res\layout\activity_main.xml
How do you fix this error?
In the example the culprit is "android:layout_height" of the ListView defined above com.google.android.maps.MapView. The "android:layout_height" of the ListView is set to "fill_parent" and therefore the ListView fills up all the remaining space such that there's no space for com.google.android.maps.MapView.

In fact usually 'fill_parent' is the culprit in your layout. Simply set android:layout_width and android:layout_height to a fixed value such as 1dp and you should be able to see com.google.android.maps.MapView loaded fine!
If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael