Android MapView always raises OutOfMemoryError in nested elements

I am trying to create a MapView (currently without any overlays) inside some nested elements. This is basically something like ScrollView -> RelativeLayout -> RelativeLayout -> MapView

  <com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="420px" android:layout_height="300px" android:clickable="false" android:apiKey="key"/> 

Everything seems to be in order. There is nothing more than what I do with it at startup, but it always calls the following:

 04-04 13:38:33.910: WARN/dalvikvm(13628): threadid=1: thread exiting with uncaught exception (group=0x40015560) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): FATAL EXCEPTION: main 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): java.lang.OutOfMemoryError 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android_maps_conflict_avoidance.com.google.googlenav.map.Map.resize(Map.java:1368) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android_maps_conflict_avoidance.com.google.googlenav.map.Map.resize(Map.java:1337) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at com.google.android.maps.MapView.onMeasure(MapView.java:590) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.View.measure(View.java:8313) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.View.measure(View.java:8313) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.View.measure(View.java:8313) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1072) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.ScrollView.onMeasure(ScrollView.java:296) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.View.measure(View.java:8313) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.View.measure(View.java:8313) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.View.measure(View.java:8313) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.View.measure(View.java:8313) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.ViewRoot.performTraversals(ViewRoot.java:839) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.os.Handler.dispatchMessage(Handler.java:99) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.os.Looper.loop(Looper.java:123) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at android.app.ActivityThread.main(ActivityThread.java:3647) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at java.lang.reflect.Method.invokeNative(Native Method) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at java.lang.reflect.Method.invoke(Method.java:507) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-04 13:38:33.996: ERROR/AndroidRuntime(13628): at dalvik.system.NativeStart.main(Native Method) 04-04 13:38:34.027: WARN/ActivityManager(6000): Force finishing activity package/.home 04-04 13:38:34.527: WARN/ActivityManager(6000): Activity pause timeout for HistoryRecord{405336d0 package/.home} 

Do you have an idea why this is so and what can I do to get rid of it? I cannot bear this from this point of view, because this is where I need it.

Thanks!

+8
android out-of-memory android-mapview
source share
7 answers

I learned something very simple.

Just place the MapView inside FrameLayout. Let FramgeLayout be wrap_content | wrap_content and everything is fine :)

This fails:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <view android:layout_width="100dip" android:layout_height="90dip" class="com.google.android.maps.MapView" android:apiKey="@string/API_KEY" android:clickable="true" /> </RelativeLayout> </ScrollView> </RelativeLayout> 

I get the following stack:

 java.lang.OutOfMemoryError at com.google.googlenav.map.Map.resize(Unknown Source) at com.google.android.maps.MapView.onMeasure(MapView.java:554) at android.view.View.measure(View.java:8172) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362) at android.view.View.measure(View.java:8172) at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:989) at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) at android.widget.ScrollView.onMeasure(ScrollView.java:286) at android.view.View.measure(View.java:8172) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362) at android.view.View.measure(View.java:8172) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140) at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) at android.view.View.measure(View.java:8172) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140) at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) at android.view.View.measure(View.java:8172) at android.view.ViewRoot.performTraversals(ViewRoot.java:805) at android.view.ViewRoot.handleMessage(ViewRoot.java:1744) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:144) at android.app.ActivityThread.main(ActivityThread.java:4937) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method) 

Here's the solution:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <view android:layout_width="100dip" android:layout_height="90dip" class="com.google.android.maps.MapView" android:apiKey="@string/API_KEY" android:clickable="true" /> </FrameLayout> </RelativeLayout> </ScrollView> </RelativeLayout> 

Hello Marco

+11
source share

I also had this problem, this happens when you try to specify the exact dimensions of the map. Set one of the fill_parent or wrap_content values, and the other is any dimension you want. That should do the trick.

+6
source share

I really can't see where you get OutOfMemoryError? I see it:

 Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity. 

Which says that you can create a MapView inside a class that extends MapActivity, and that means you are not doing this?

+2
source share

Take a look at the following thread:

Issue 2181: System memory leak when using MapView

This seems to be something internal to MapActivity. The thread has been alive for more than two years, and it was not fixed as fixed ...

Edit: I tried the solution described there, and this led me to a similar solution:

 try { Field mConfigField = MapActivity.class.getDeclaredField("mConfig"); mConfigField.setAccessible(true); Object mConfig = mConfigField.get(this); if (null != mConfig) { Field mConfigContextField = mConfig.getClass().getDeclaredField("context"); mConfigContextField.setAccessible(true); mConfigContextField.set(mConfig, null); mConfigField.set(this, null); } } catch (Exception ex) { // Do error handling } 
+2
source share

This is actually caused by the placement of MapView in Scrollview. See Android . MapView always raises an OutOfMemoryError in nested elements for more information.

+1
source share

This is actually due to the fact that Scrollview creates a buffer that should contain a view in every possible position of the scrollbar to ensure fast and smooth scrolling. Since MapView is quite large when tiles are loaded during the measurement phase, the resulting buffer created by ScrollView can be huge (about 128 M). If you have not determined that your emulator has more than 256 M memory and empty space, it will fail with an error outside the memory. You can configure your emulator for 512 MB of memory and heaps, but your application will crash on any device that has less than 512 MB of memory or not enough space for heaps. It is best to remove MapView from ScrollView.

0
source share

All Articles