缩放时,Android MapView会出现内存不足错误

时间:2012-09-03 08:28:50

标签: android android-mapview out-of-memory

我有一个应用程序,它使用地图活动作为整个应用程序的一部分,当我放大和缩小时,我得到内存不足错误。我只有一个叠加项目。不知道怎么解决这个问题?这是我的下面的xml,它是完全嵌套的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/contact_layout" >

<com.google.android.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:apiKey="..."
    android:clickable="true" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:paddingBottom="20dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="20dp"
    android:weightSum="100" >

    <ImageView
        android:id="@+id/phone_icon"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:adjustViewBounds="true"
        android:src="@drawable/phone_icon" />

    <ImageView
        android:id="@+id/email_icon"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:adjustViewBounds="true"
        android:src="@drawable/mail_icon" />

    <ImageView
        android:id="@+id/bus_icon"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:adjustViewBounds="true"
        android:src="@drawable/bus_icon" />

    <ImageView
        android:id="@+id/twitter_icon"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:adjustViewBounds="true"
        android:src="@drawable/twitter_icon" />

    <ImageView
        android:id="@+id/facebook_icon"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20"
        android:adjustViewBounds="true"
        android:src="@drawable/facebook_icon" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/navigation_bar_without_back"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:background="@drawable/header_navigation"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/anatolia_logo"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="-10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="-10dp"
        android:adjustViewBounds="true"
        android:src="@drawable/..." />

    <ImageView
        android:id="@+id/call_button_without_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:adjustViewBounds="true"
        android:src="@drawable/call_button" />
</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_below="@+id/navigation_bar_without_back"
    android:background="@drawable/contact_layout_background" >

    <WebView
        android:id="@+id/info_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

</RelativeLayout>

LogCat Trace

09-03 11:43:20.752: E/AndroidRuntime(930): FATAL EXCEPTION: main
09-03 11:43:20.752: E/AndroidRuntime(930): java.lang.OutOfMemoryError: bitmap size   exceeds VM budget
09-03 11:43:20.752: E/AndroidRuntime(930):  at   android.graphics.Bitmap.nativeCreate(Native Method) 
09-03 11:43:20.752: E/AndroidRuntime(930):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
09-03 11:43:20.752: E/AndroidRuntime(930):  at     com.google.android.maps.ZoomHelper.createSnapshot(ZoomHelper.java:444)
09-03 11:43:20.752: E/AndroidRuntime(930):  at com.google.android.maps.ZoomHelper.doZoom(ZoomHelper.java:151)
09-03 11:43:20.752: E/AndroidRuntime(930):  at com.google.android.maps.ZoomHelper.doZoom(ZoomHelper.java:140)
09-03 11:43:20.752: E/AndroidRuntime(930):  at com.google.android.maps.MapView.doZoom(MapView.java:1517)
09-03 11:43:20.752: E/AndroidRuntime(930):  at com.google.android.maps.MapView.doZoom(MapView.java:1526)
09-03 11:43:20.752: E/AndroidRuntime(930):  at com.google.android.maps.MapView$6.onZoom(MapView.java:1481)
09-03 11:43:20.752: E/AndroidRuntime(930):  at android.widget.ZoomButtonsController$4.onClick(ZoomButtonsController.java:274)
09-03 11:43:20.752: E/AndroidRuntime(930):  at android.view.View.performClick(View.java:2485)
09-03 11:43:20.752: E/AndroidRuntime(930):  at andrd.view.View$PerformClick.run(View.java:9080)
09-03 11:43:20.752: E/AndroidRuntime(930):  at android.os.Handler.handleCallback(Handler.java:587)
09-03 11:43:20.752: E/AndroidRuntime(930):  at andrid.os.Handler.dispatchMessage(Handler.java:92)
09-03 11:43:20.752: E/AndroidRuntime(930):  at android.os.Looper.loop(Looper.java:130)
09-03 11:43:20.752: E/AndroidRuntime(930):  at android.app.ActivityThread.main(ActivityThread.java:3683)
     09-03 11:43:20.752: E/AndroidRuntime(930):     at java.lang.reflect.Method.invokeNative(Native Method)
    09-03 11:43:20.752: E/AndroidRuntime(930):  at java.lang.reflect.Method.invoke(Method.java:507)
    09-03 11:43:20.752: E/AndroidRuntime(930):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    09-03 11:43:20.752: E/AndroidRuntime(930):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    09-03 11:43:20.752: E/AndroidRuntime(930):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

您的布局中嵌套了很多ImageView。 不幸的是,图像加载和位图可以是PITA。 已知ImageView会导致OutOfMemoryErrors。特别是当他们从代码中填充Bitmaps时。

要解决此问题,请删除ImageView并检查错误是否仍然存在。

如果它解决了错误,请重新考虑在MapView上显示图像的策略。同样使用density-dependent资源可能有助于在一定程度上解决问题。

编辑:在查看堆栈跟踪后,其中显示:“位图超出VM预算”我确信您的图像加载策略需要改进。