android MapView always causes an OutOfMemoryError in nested elements

后端 未结 7 1413
我在风中等你
我在风中等你 2020-12-03 12:55

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

相关标签:
7条回答
  • 2020-12-03 13:00

    I found out something quite simple.

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

    This one 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 stacktrace:

    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)
    

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

    Greetings, Marco

    0 讨论(0)
  • 2020-12-03 13:00

    I had this problem as well, it occurs when you try to specify the exact dimensions of a mapview. Set one of the dimensions to fill_parent or wrap_content and let the other be whatever dimension you want. That should do the trick.

    0 讨论(0)
  • 2020-12-03 13:07

    Take a look at the following thread:

    Issue 2181:Memory leak in system when using MapView

    Seems like this is something internal in MapActivity. The thread is alive for over two years now and it was not reported as fixed...

    Edit: tried the solution described there and it 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
        }
    
    0 讨论(0)
  • 2020-12-03 13:07

    steemcb is correct, but unfortunately there is also a bug in MapView that causes this to happen even if your layout is good. I got the exact same stack trace as yours. It is a fragile component and small changes in layout type will cause it to break unexpectedly, especially if it's wrapped in a ScrollLayout.

    It hasn't yet been addressed by anyone from the Android project.

    0 讨论(0)
  • 2020-12-03 13:08

    Actually its caused by placing the MapView in a Scrollview. See android MapView always causes an OutOfMemoryError in nested elements for more information.

    0 讨论(0)
  • 2020-12-03 13:14

    I don't really see where you are getting an OutOfMemoryError? I do see this:

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

    Which says you can only create a MapView inside a class that extends MapActivity, and it implies that you do not do this?

    0 讨论(0)
提交回复
热议问题