2012-03-03 103 views
0

我創建使用mapActivity和MapView的一個應用程序並遇到怪異消息:
「E/AndroidRuntime(4276):java.lang.IllegalArgumentException異常:寬度和高度必須> 0」MapActivity - 寬度和高度必須> 0

該消息並不總是顯示出來。我回溯了它出現的情況,發現這個:

1)如果我已經很長時間觸摸應用程序並運行它,崩潰將顯示,請注意,如果在崩潰後我重新打開應用程序,然後有一次NO崩潰。

2)如果我刪除memrey(RAM),然後在我運行應用程序時就會死機。

3)如果我跑dibugg模式的話,我從來沒有遇到死機...

從我的角度來看,我認爲有關的東西墜毀的Android還不misured但仍試圖顯示它。 可是我還是不碰的MapView MISURE參數,所以我爲什麼要感到這樣會崩潰嗎? 反正,我不知道該怎麼做,因爲debugg模式不能真的幫助這裏。 我會展示我的onCreate()在這裏:

/******************************** Methods ***************************************** 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); //get rid of the title bar of the window, NOTE: we have to do this BEFORE we call "setContentView()" 
    //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); //this line is use to remove the uppermost bar in the window (the one with the time , etc.) 

    setContentView(R.layout.create_map); 
    mMapView = (MapView)findViewById(R.id.mapview); 
    moveDotBtn = (Button)findViewById(R.id.movedot); 
    finishBuildBtn = (Button)findViewById(R.id.finish); 

    mapController = mMapView.getController(); 
    mMapOverlays = mMapView.getOverlays(); 
    mMapView.setBuiltInZoomControls(true); 
    mMapView.setSatellite(false); 
    mMapView.setStreetView(false); 

    whereAmI = new MyCustomLocationOverlay(this, mMapView,mapController); 
    mMapView.getOverlays().add(whereAmI); 
    //mMapView.postInvalidate(); //without that call the overlay "where_m_i" will not show. 

    sensor_mgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //activate for compass 
    sensor = sensor_mgr.getDefaultSensor(Sensor.TYPE_ORIENTATION); //activate for compass, and orientation 

    powerManager =(PowerManager)getSystemService(Context.POWER_SERVICE); //to make always on, and never looked 
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock"); 

    locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    lastLocation = null; 


    Touchy t = new Touchy(); 
    mMapOverlays.add(t); 

    iconItems = getResources().getStringArray(R.array.icons); 
    RES = getResources(); 
    mIconOverlays = new IconItemizedOverlay(RES.getDrawable(R.drawable.ic_bench)); 
    mMapOverlays.add(mIconOverlays); 

    mapController.setZoom(17); 
} 

@Override 
public void onResume() 
{ 
    wakeLock.acquire(); //make your phone awake at all times. 

    locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0 , whereAmI); 
    locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0 , whereAmI); 
    whereAmI.enableMyLocation(); 
    whereAmI.enableCompass(); 
    findLastKnownLocation(); 
    sensor_mgr.registerListener(sensorEventListener , sensor ,SensorManager.SENSOR_DELAY_NORMAL); 
    super.onResume(); 
} 

回答

0

我想我得到了答案,但我不知道,只有時間能與像thoes巴格斯告訴。它好像在使用谷歌地圖我不應該觸摸屏幕上的onCreate,所以如果我想要做ZoomIn或setZoom(SOME_NUMBER),那麼我應該做它的的onResume(),而不是的onCreate。希望這會幫助別人,有一天:) BTW: ,如果有人有一個想法,如果是真的的問題,或者是它只是一個temporery的解決方案的話,我想聽聽,casue它似乎做工精細用我的解決方案:)

0

你應該做你的變化onWindowFocusChanged因爲你沒有在那之前獲分配的屏幕高度/寬度。

@Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     if (hasFocus) { 
      setOverlay(); //Your code should come here 
     } 
     } 
     super.onWindowFocusChanged(hasFocus); 
    } 
相關問題