Android map 显示内存不足错误

标签 android google-maps

我在我的 android 应用程序中使用谷歌地图。我在 xml 文件中创建了一个 ma​​pview 它工作正常但是当我将 mapview 放入 scrollview 时它显示 OutOfMemoryError 和崩溃。

这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg" >

    <RelativeLayout
        android:id="@+id/main_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:id="@+id/header_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/share_header" >

            <Button
                android:id="@+id/back_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:background="@drawable/back" />

            <TextView
                android:id="@+id/header_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textStyle="bold" />

            <Button
                android:id="@+id/post_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:background="@drawable/post" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/share_leave_note_layout"
            android:layout_width="wrap_content"
            android:layout_height="150dip"
            android:layout_below="@+id/header_layout"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="5dip" >

            <ImageView
                android:id="@+id/sharer_img"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:background="@drawable/img_box" />

            <RelativeLayout
                android:id="@+id/leave_note_layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/sharer_img"
                android:background="@drawable/comment_left_arrow_extra_big" >

                <EditText
                    android:id="@+id/leave_note_edt"
                    android:layout_width="170dip"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dip"
                    android:layout_marginRight="15dip"
                    android:layout_marginTop="10dip"
                    android:background="@null"
                    android:hint="@string/laeve_a_note_text" />

                <ImageView
                    android:id="@+id/captured_img"
                    android:layout_width="40dip"
                    android:layout_height="40dip"
                    android:layout_margin="10dip"
                    android:layout_toRightOf="@+id/leave_note_edt"
                    android:background="@drawable/img_box" />
            </RelativeLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/iamat_and_with_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/share_leave_note_layout"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="5dip"
            android:gravity="center_vertical" >

            <Button
                android:id="@+id/web_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/web" />

            <Button
                android:id="@+id/iam_at_btn"
                android:layout_width="90dip"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/web_btn"
                android:background="@drawable/i_m_at" />

            <Button
                android:id="@+id/contact_logo_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/iam_at_btn"
                android:background="@drawable/contact_group" />

            <Button
                android:id="@+id/iam_with_btn"
                android:layout_width="90dip"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/contact_logo_btn"
                android:background="@drawable/i_m_with" />
        </RelativeLayout>

        <FrameLayout
            android:id="@+id/map_contact_layout"
            android:layout_width="150dip"
            android:layout_height="150dip"
            android:layout_below="@+id/iamat_and_with_layout"
            android:layout_marginLeft="10dip"
            android:layout_marginTop="5dip"
            android:background="@drawable/rectangle_img"
            android:gravity="center_vertical" >

            <com.google.android.maps.MapView
                android:id="@+id/mapView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:apiKey="0_oThqu6ib8WYroWAckbRjYAkaoeMUX-52diovQ"
                android:clickable="true"
                android:enabled="true" />
        </FrameLayout>

        <RelativeLayout
            android:id="@+id/contact_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/iamat_and_with_layout"
            android:layout_toRightOf="@+id/map_contact_layout"
            android:gravity="center_vertical" >

            <ImageView
                android:id="@+id/contact_img1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:background="@drawable/square" />

            <ImageView
                android:id="@+id/contact_img2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:layout_toRightOf="@+id/contact_img1"
                android:background="@drawable/square" />

            <ImageView
                android:id="@+id/contact_img3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/contact_img1"
                android:layout_margin="5dip"
                android:background="@drawable/square" />

            <ImageView
                android:id="@+id/contact_img4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/contact_img2"
                android:layout_margin="5dip"
                android:layout_toRightOf="@+id/contact_img3"
                android:background="@drawable/square" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/fb_twitter_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/contact_layout"
            android:layout_marginRight="5dip"
            android:background="@null" >

            <ImageView
                android:id="@+id/fb_fade_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:background="@drawable/fb_fade" />

            <ImageView
                android:id="@+id/twitter_fade_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dip"
                android:layout_toRightOf="@+id/fb_fade_icon"
                android:background="@drawable/twitter_fade" />
        </RelativeLayout>
    </RelativeLayout>

</ScrollView>

最佳答案

试试这个。

<?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="0_oThqu6ib8WYroWAckbRjYAkaoeMUX-52diovQ" android:clickable="true" />

            </FrameLayout>

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

PS:最新的 map 版本是 Google Maps Android API v2 .尝试使用它。这里有个好tutorial

关于Android map 显示内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16142150/

相关文章:

java - Android ArrayList - 逐一添加项目会导致 IndexOutOfBoundsException

Android 将 base64 字符串传递给另一个有 Intent 的 Activity

javascript - 谷歌地图拖尾事件未定义,而点击有效

android - 谷歌地图 - 内存不足异常

android - 谷歌地图自行车路线 Intent

javascript - Google map /TypeScript - 无法一致识别 google.maps.Place

java - 如何从 Firebase 检索数据并将其设置为图表的 string 和 int

java - 在 Android 中保存 Drag 'n' Drop in RecyclerView 后所做的更改 - 我已阅读所有内容

android - SphericalUtil.computeDistanceBetween() 与 Location.distanceBetween()

java - 如何通过按钮将 Activity 链接到 Fragment