2014-03-04 115 views
4

我有數據列表視圖,每個項目打開一個嵌入了地圖的視圖。這張地圖的目的是顯示一個標記。代碼工作正常,但如果我有很多列表項(50+),來回騎車會導致應用程序拋出OutOfMemory異常。Android Google Maps API V2 OutOfMemory異常

有人可以幫助我瞭解我在做什麼錯誤在這裏..我猜測,我的地圖初始化邏輯中有什麼問題導致這..我試圖弄清楚。

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) 

回答

0

是你的工作是什麼Android版本?因爲在較早的(2.3-)Android版本中,Bitmap不會被垃圾收集器自動清除。也檢查出這個答案:Bitmap, Bitmap.recycle(), WeakReferences, and Garbage Collection

+0

我的minSdkVersion是10和targetSdkVersion是17 .. – Sharath

+0

SDK 10是Android 2.3.3。所以位圖的處理方式與3.0和更高版本不同。請閱讀Google上的鏈接或查找「Bitmap clean Android」以獲取適當的答案。 –

+0

嗯......你可能是正確的..讓我碰到版本,並試試這個.. – Sharath