Android Google Maps API V2 OutOfMemory例外

时间:2014-03-04 10:50:43

标签: android google-maps

我有数据列表视图,每个项目都会打开一个嵌入了地图的视图。此地图的目的是显示单个标记。代码工作正常,但如果我有很多列表项(50+),来回循环会导致应用程序抛出OutOfMemory异常。

有人可以帮助我理解我在这里做错了什么..我猜我的Map初始化逻辑中有一些错误导致了这个...而我正在试图解决这个问题。

public class DetailView extends Fragment implements OnInfoWindowClickListener {

    private MapView mMapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {

            mMapView = (MapView) v.findViewById(R.id.mapBusinessLocation);
            mMapView.onCreate(savedInstanceState);
            mMapView.onResume();//needed to get the map to display immediately

            MapsInitializer.initialize(this.getActivity() );
            //MapController myMapController = mMapView.getController();
            googleMap = mMapView.getMap();
            googleMap.setMyLocationEnabled(true);
            CameraUpdate center=
                    CameraUpdateFactory.newLatLng(new LatLng(-20.269927,57.672729)); 
             CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

            googleMap.moveCamera(center);
            googleMap.animateCamera(zoom); 
    }

       @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

        placeMarker("test");

        }

        private Marker placeMarker(String title) {
            //Get the hue
            String pinColor = "#f29217";    
            int c = Color.parseColor(pinColor);
            float[] pixelHSV = new float[3];
            Color.colorToHSV(c, pixelHSV);
            return googleMap.addMarker(new MarkerOptions()
             .position(new LatLng(-20.269927,57.672729 ))
             .title(title)
             .snippet("")
             .icon(BitmapDescriptorFactory.defaultMarker(pixelHSV[0])));
        }
           @Override
        public void onResume() {
            super.onResume();

                if(mMapView != null) {
                mMapView.onResume();
                }
        }

        @Override
        public void onPause() {
            super.onPause();

                if(mMapView != null) {
                mMapView.onPause();
                }
        }

        @Override
        public void onDestroy() {
            super.onDestroy();

                if(mMapView != null) {
                mMapView.onDestroy();
                }

        }

        @Override
        public void onLowMemory() {
            super.onLowMemory();

                if(mMapView != null) {
                mMapView.onLowMemory();
                }
        }

}

下面给出了应用崩溃时的堆栈跟踪..

    PP_VERSION_CODE=53
    ANDROID_VERSION=4.3
    PHONE_MODEL=GT-I9505
    CUSTOM_DATA=
    STACK_TRACE=java.lang.OutOfMemoryError
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:670)
    at maps.al.k.a(Unknown Source)
    at maps.as.b.a(Unknown Source)
    at maps.as.b.a(Unknown Source)
    at maps.as.b.b(Unknown Source)
    at maps.au.al.a(Unknown Source)
    at maps.au.at.a(Unknown Source)
    at maps.ay.ap.a(Unknown Source)
    at maps.ap.f.a(Unknown Source)
    at maps.ap.f.b(Unknown Source)
    at maps.aj.y.l(Unknown Source)
    at maps.aj.y.run(Unknown Source)

    LOGCAT=03-03 16:26:47.204 E/dalvikvm-heap(15112): Out of memory on a 4194320-byte allocation.
    03-03 16:26:47.204 I/dalvikvm(15112): "GLThread 911" prio=5 tid=29 RUNNABLE
    03-03 16:26:47.204 I/dalvikvm(15112):   | group="main" sCount=0 dsCount=0 obj=0x478125e0 self=0x79d89680
    03-03 16:26:47.204 I/dalvikvm(15112):   | sysTid=15717 nice=1 sched=0/0 cgrp=apps handle=1982454736
    03-03 16:26:47.204 I/dalvikvm(15112):   | state=R schedstat=( 0 0 0 ) utm=47 stm=8 core=1
    03-03 16:26:47.204 I/dalvikvm(15112):   at android.graphics.Bitmap.nativeCreate(Native Method)
    03-03 16:26:47.204 I/dalvikvm(15112):   at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
    03-03 16:26:47.204 I/dalvikvm(15112):   at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
    03-03 16:26:47.204 I/dalvikvm(15112):   at android.graphics.Bitmap.createBitmap(Bitmap.java:670)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.al.k.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.as.b.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.as.b.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.as.b.b((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.au.al.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.au.at.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.ay.ap.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.ap.f.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.ap.f.b((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.aj.y.l((null):-1)

03-03 16:26:47.204 I/dalvikvm(15112):   at maps.aj.y.run((null):-1)

1 个答案:

答案 0 :(得分:0)

你在做什么Android版本?因为在早期(2.3-)Android版本中,垃圾收集器不会自动清理Bitmap。另请查看此答案:Bitmap, Bitmap.recycle(), WeakReferences, and Garbage Collection