开发者

Adding buttons on xml file other than main

开发者 https://www.devze.com 2023-03-05 07:31 出处:网络
I have two xml files in my applic开发者_如何学Goation - main.xml and options.xml. In both of them I use buttons. The problem is, that while interacting with the buttons in main.xml, I cannot do that

I have two xml files in my applic开发者_如何学Goation - main.xml and options.xml.

In both of them I use buttons. The problem is, that while interacting with the buttons in main.xml, I cannot do that with options.xml: if I write

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

, b is going to be null. What is the cause of this problem and how do I fix it?

Thank you in advance.


You need to either inflate the options.xml or set it as content view:

setContentView(R.layout.options);

before you can use the views in that layout file.

It sounds like you want to be able to access both layouts so you should do something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号