2012-03-03 105 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); 
    } 
相关问题