2012-04-05 138 views
3

我有以下代碼:Android的MapView類嘗試添加視圖,但已經有家長

setContentView(R.layout.maplayout); 
// Create Rotate view 
mRotateView = new RotateView(this); 

// create a map view 
mapView = (MapView) findViewById(R.id.mapview); 

mRotateView.addView(mapView); // error here 
setContentView(mRotateView); 
mylocation = new MyLocationOverlay(this,mapView); 

但是,當我加的觀點,我得到的是說,指定的子已經有母的錯誤。我認爲這是因爲mapView已經是佈局的一個孩子。

那麼,我該如何解決這個問題呢?

回答

0

選項#1:將RotateView作爲MapView小部件的父級資源放置在res/layout/maplayout.xml資源中,因此您不必在Java代碼中設置它。

選項2:從您的res/layout/maplayout.xml資源中刪除MapView,通過其構造函數創建它的一個實例,並將它添加到您的RotateView的Java代碼中。

選項3:在調用addView()將其添加到RotateView之前,請在父級上調用removeView()以從中刪除MapView

相關問題