MapActivity - 宽度和高度必须> 0

时间:2012-03-03 10:48:07

标签: android google-maps android-mapview

我使用mapActivity和MapView创建了一个应用程序并遇到了奇怪的消息:
“E / AndroidRuntime(4276):java.lang.IllegalArgumentException:width和height必须是> 0”

该消息并不总是显示出来。我回溯了它出现的情况并发现了这个:

1)如果我触摸应用程序并运行它已经很长时间了,崩溃就会显示出来,请注意,如果在崩溃后我重新打开应用程序,那么 NO 再次崩溃。

2)如果我删除了memrey(RAM),那么在我运行应用程序后它会崩溃。

3)如果我运行dibugg模式,那么我从未遇到过崩溃......

从我的角度来看,我认为崩溃与Android尚未发现但仍试图显示它的内容有关。 仍然我不触摸mapview misure参数,所以为什么我应该得到这个崩溃? 无论如何,我不知道该怎么做,因为调试模式不能在这里真正帮助。 我将在这里显示我的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();
}

2 个答案:

答案 0 :(得分:0)

我想我得到了答案,但我不确定,只有时间可以告诉像虫子这样的bug。看起来好像我不应该在使用谷歌地图时触摸onCreate上的屏幕,所以如果我想做ZoomIn或setZoom(SOME_NUMBER),那么我应该在onResume()而不是onCreate上进行。希望这会有所帮助,有一天:) BTW: 如果有人知道它是否真的是问题,或者它只是一个临时解决方案,那么我想听听,casue它似乎与我的解决方案一起工作:)

答案 1 :(得分:0)

您应该在onWindowFocusChanged中进行更改,因为在此之前您没有分配屏幕高度/宽度。

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