简体   繁体   中英

Android - Get Other Layout Widget

I've been trying to get a widget from another layout for past hours but still no luck. Can someone tell what is wrong with my current code?

public void refreshClicked(View view){

    tab1 = LayoutInflater.from(view.getContext()).inflate(R.layout.tab_1, null);
    Button b = (Button) tab1.findViewById(R.id.refresh);
    b.setText("Updating...");


}

I've hooked my button onClick to refreshClicked(View view) method.

This method is under my MainActivity.java and I'm trying to get the Button with id refresh from tab_1.xml layout but the button text is not updating.

Thanks!

You seems to inflate a layout but never attach it to a parent view as the ViewGroup parameter is null. Then this layout inflated must not even been displayed anywhere.

If the button b already exists, no need to inflate the layout and retrieve the button form there, it will a different button. Just retrieve the button from the actual existing layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM