16

I'm a novice android programmer.

Let me give some context.
My project uses ActionBarSherlock.

  1. I have an activity (Activity1) that contains two fragments - a SherlockListFragment and a SherlockFragment containing a google maps MapView (essentially a MapFragment).
  2. Search results are displayed in the list fragment and the map fragment, and an action bar options button toggles the currently showing Fragment between the list and the map.
  3. Selecting an item in the list or the map of search results starts a new activity (Activity2) displaying data from the selected item.
  4. This new activity has a similar flow: a fragment to show the data, and a map fragment to show a location - toggled by an action bar options button.

The problem:

  1. I can select a list item in the search results (Activity1) list fragment and the details page (Activity2) is started successfully.
  2. When I select a map item from the map overlay in (Activity1), the app crashes, and I get the stacktrace shown below.

What I've tried so far:

  1. I've searched for "map has zero size" and many permutations of the same search including "android" and "illegalstateexception" and various lines of the stacktrace and I've examined results that don't even relate to android but relate to the google maps apis in general, and I have not been able to find anything or anyone anywhere who has encountered this error except for at this url http://www.androidpub.com/1551654 But I don't know korean, and Google translate wasn't much help.
  2. I've tried to find some snippets of the source code to see what's going on, but that turned up nothing.
  3. I've added logs in my code to try and pinpoint where the exception occurs, and all that I've been able to find is that it occurs sometime after (Activity2) the details activity starts, that is, after onCreate() is called. And both the details fragment and map fragment have booth been successfully instantiated within onCreate(). I haven't been able to find a place in my code to even catch the exception. So for all that I can see, this exception appears to be entirely a bug in google's MapView code, but if I've made a mistake anywhere, please do show it. I'm open to help from anyone who can tell me what is going on here.

So, in summary. The crash occurs when going from an activity (Activity1) with a currently showing mapview to another activity (Activity2) that also hosts a fragment containing another mapview. But the crash does not occur when going from the list fragment showing in (Activity1) to the other activity (Activity2) containing another map fragment.

I hope this is enough detail. If you have any questions, comment please.

D/memalloc(  121): /dev/pmem: Allocated buffer base:0x4215c000 size:2088960 offset:4177920 fd:66
D/memalloc(30944): /dev/pmem: Mapped buffer base:0x5b4f5000 size:6266880 offset:4177920 fd:164
D/AndroidRuntime(30944): Shutting down VM
W/dalvikvm(30944): threadid=1: thread exiting with uncaught exception (group=0x40aaa228)
E/AndroidRuntime(30944): FATAL EXCEPTION: main
E/AndroidRuntime(30944): java.lang.IllegalStateException: Map has zero size
E/AndroidRuntime(30944):    at android_maps_conflict_avoidance.com.google.googlenav.map.Map.drawMap(Map.java:818)
E/AndroidRuntime(30944):    at com.google.android.maps.MapView.drawMap(MapView.java:1091)
E/AndroidRuntime(30944):    at com.google.android.maps.MapView.onDraw(MapView.java:522)
E/AndroidRuntime(30944):    at android.view.View.draw(View.java:11071)
E/AndroidRuntime(30944):    at android.view.View.getDisplayList(View.java:10462)
E/AndroidRuntime(30944):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2605)
E/AndroidRuntime(30944):    at android.view.View.getDisplayList(View.java:10425)
E/AndroidRuntime(30944):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2605)
E/AndroidRuntime(30944):    at android.view.View.getDisplayList(View.java:10425)
E/AndroidRuntime(30944):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2605)
E/AndroidRuntime(30944):    at android.view.View.getDisplayList(View.java:10425)
E/AndroidRuntime(30944):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2605)
E/AndroidRuntime(30944):    at android.view.View.getDisplayList(View.java:10425)
E/AndroidRuntime(30944):    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2605)
E/AndroidRuntime(30944):    at android.view.View.getDisplayList(View.java:10425)
E/AndroidRuntime(30944):    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:879)
E/AndroidRuntime(30944):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:1948)
E/AndroidRuntime(30944):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1654)
E/AndroidRuntime(30944):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2498)
E/AndroidRuntime(30944):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(30944):    at android.os.Looper.loop(Looper.java:154)
E/AndroidRuntime(30944):    at android.app.ActivityThread.main(ActivityThread.java:4894)
E/AndroidRuntime(30944):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(30944):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(30944):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(30944):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(30944):    at dalvik.system.NativeStart.main(Native Method)
E/EmbeddedLogger(  254): App crashed! Process: com.myapp.android
E/EmbeddedLogger(  254): App crashed! Package: com.myapp.android v5 (2.0)
E/EmbeddedLogger(  254): Application Label: myapp
W/ActivityManager(  254):   Force finishing activity com.myapp.android/.activity.InfoActivity
W/ActivityManager(  254):   Force finishing activity com.myapp.android/.activity.SearchActivity
Adam J
  • 161
  • 1
  • 5
  • 1
    I found this thread while searching for the same errormsg "Map has zero size", I found out that I've accidently set another item to "match_parent" instead of "wrap_content" and thus pushing the mapview of screen - resulting in this error. Are you sure that your layout really can display the MapView? – Fluff Jul 10 '12 at 16:08
  • I have faced the same problem, for my case I had done some changes in XML file – Sridhar Aug 03 '12 at 09:37
  • are you using different process for the two activities containing mapview. http://stackoverflow.com/questions/3379575/how-to-use-multiple-mapactivities-mapviews-per-android-application-process/3380655#3380655 – Gaurav Vashisth Aug 29 '12 at 16:13
  • Can you please post your code as well? – Nicholas Sep 16 '12 at 08:37
  • The project I'm working on is closed source, and I've since changed the code. But to give an update, I ended up using just one activity with one MapView, and used an integer passed via intent as a mode to indicate what views and behavior I wanted from the activity. It was a hack, but I needed to finish the project quickly, and it worked fine, but it was messy code. I also noticed afterward that when I attempted to animate the MapView (I was doing a slide in animation), the same error occured, but when I animated the other views in the layout and left the MapView unmoved, no exception occured. – Adam J Sep 20 '12 at 00:29
  • As to using processes, I did try using separate processes for both of the activities, and if I remember correctly, that did work, but there was always a black screen and a 1 or 2 second delay when starting the second activity's process. I wasn't satisfied with this performance, so I followed the method I mentioned in my last comment. – Adam J Sep 20 '12 at 00:33
  • Since this post, I've found that the most effective way to toggle between a map and another view is to have the MapActivity manage the MapView and a fragment manage the other View. I reimplemented the same feature again, and this has worked with no problems. – Adam J Nov 27 '12 at 14:58
  • I encountered this problem while using android-tandemactivities, which has a MapFragment implementation. The problem was a container of the MapView had wrap_content instead of match_parent - MapView draws within it's size so it can't provide a size of its own to parent containers. – straya Nov 30 '12 at 13:43
  • @AdamJ if you have found a satisfactory fix, please post as detailed an answer as you can to the question and mark it as the accepted answer, please. – MattC Jan 14 '13 at 16:47
  • Just a shot in the dark; are you sure you're doing your updates from the UI thread? The bug looks almost like a race condition between threads. – Edward Falk Jan 15 '13 at 01:01
  • As far as I know, my threading was correct. – Adam J Jan 24 '13 at 15:39

2 Answers2

0

At this point in my experience, the only way to avoid this bug, is to not use MapViews inside Fragments. Moving a MapView around at all by whether animation or tampering with it's position once it's been added to a parent view has always caused me problems. The most effective way I've found is to toggle between a map and another view where the MapActivity manages the MapView and a fragment manages the other View. I reimplemented the same feature again, and this has worked with no problems. However, I would observe that this is a workaround for an issue with an unknown cause (very likely a bug in the android maps API).

Adam J
  • 161
  • 1
  • 5
0

I absolutely disagree with Adam's! Managing fragments and activities is the best practice ever.

It's quite different, and sometimes it's even harder to work out something, but managing a map fragment as a fragment itself, or inside of another fragment to be used by several activities is the best you can do if you plan to include maps in several views of your app.

So, going to the point, since you did not posted any code, I guess you are animating a camera may be to reset the boundaries, or may be just setting up an initial center for your map. (most likely first one since you are showing results)

And in that case, the exception would be in the map.animateCamera(...) line, because you are dealing with something that is available but it's not inflated yet.

try this one:

try{
        cu = CameraUpdateFactory.newLatLngBounds(yourBoundaries,yourPadding);
        map.animateCamera(cu);
        System.out.println("Set with padding");
    } catch(IllegalStateException e) {
        e.printStackTrace();
        cu = CameraUpdateFactory.newLatLngBounds(yourBoundaries,someWidth,someHeight,zeroPadding);
        map.animateCamera(cu);
        System.out.println("Set with whp");
    }
unmultimedio
  • 1,224
  • 2
  • 13
  • 39