2011-05-11 16 views
0

我在我的應用程序中有兩個xml文件 - main.xmloptions.xml在主文件之外的xml文件上添加按鈕

在他們兩個我使用的按鈕。問題是,雖然與按鈕main.xml中互動,我不能做到這一點與options.xml:如果我寫

Button b = (Button)findViewById(R.id.b1); 

,B將是零。這個問題的原因是什麼?如何解決?

預先感謝您。

+0

也許你設置'main.xml'像'的setContentView(R.layout.main)',但是你用'options.xml'做什麼?考慮發佈一些代碼。 – ernazm 2011-05-11 13:34:15

回答

3

你需要或者膨脹options.xml或將其設置爲內容視圖:

setContentView(R.layout.options); 

,然後才能在佈局文件中使用的意見。

這聽起來像你希望能夠訪問這兩種佈局,所以你應該做這樣的事情:

View view = LayoutInflater.from(context).inflate(R.layout.options, null); 
Button b = (Button) view.findViewById(R.id.b1); 
+0

非常感謝,非常感謝! – ronash 2011-05-11 13:55:02