MapActivity - width and height must be > 0

2019-08-24 12:28发布

问题:

i created an app using mapActivity and MapView and encounter the wierd message:
" E/AndroidRuntime(4276): java.lang.IllegalArgumentException: width and height must be > 0 "

that message is not always shows up. I backtraced the situation it shows up and found out this:

1) If it's been long time since i touch the app and then run it, the crash wills show up, note that if after the crash i reopen the app then there is NO crash again.

2) if I delete the memrey (RAM) then after i run the app it will crash.

3)if I run dibugg mode then i never encounter the crash...

from my point of view i think the crash related to something that Android did not yet misured but still trying to display it. still i do not touch the mapview misure parameters so why should i ever get this crash? anyways, i have no idea what to do since the debugg mode can't realy help here. i will display my onCreate() here:

/********************************  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();
}

回答1:

i think i got the answer, but i'm not sure, only time can tell with buggs like thoes. it seems as if i should not touch the screen on the onCreate while using google maps, so if i want to do ZoomIn or setZoom(SOME_NUMBER) then i should do it on the onResume() and not the onCreate. hope this will help someone, some day :) btw: if someone has an idea if it is realy the problem or is it just a temporery solution then i would like to hear, casue it seem to work fine with my solution :)



回答2:

You should do your changes in onWindowFocusChanged since you are not alloted screen height/width before that.

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