1. 程式人生 > >View not attached to WindowManager 錯誤

View not attached to WindowManager 錯誤

今天在學習2D繪圖的時候,為了省事,在Activity的OnCreate裡寫下了如下程式碼

WindowManager wm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

wm = (WindowManager) getSystemService(Service.WINDOW_SERVICE);

DemoButton button = new DemoButton(this);
setContentView(button);
WindowManager.LayoutParams params = new       WindowManager.LayoutParams();


params.width = LayoutParams.WRAP_CONTENT;
params.height = LayoutParams.WRAP_CONTENT;
params.x = 50;
params.y = 50;

wm.updateViewLayout(button, params);


}

然後執行,報錯,View not attached to WindowManager,自己對android的這些方面的理解這樣看來真的很不到位,button並未如自己所想是attach到Window上的,WindowManager管理的是一組視窗,比如activity,dialog,以及通過addview新增到Window的懸浮窗(前提是這個view在新增之前沒有任何parent,否則會出現異常,being add,but it already has a parent),更加深刻詳細的解釋在這裡

暫時對WindowManager的理解就到這了,小弟才疏學淺,說的不對的地方請大家一定幫我指出來