Dec 10, 2012

f Comment

Fixing Android Google API Error: Map Has Zero Size

Amazon If you use 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

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 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 like the following:
    android:orientation="vertical" >

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 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

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 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