findViewById in Fragment

后端 未结 30 2271
终归单人心
终归单人心 2020-11-21 06:39

I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById<

30条回答
  •  执笔经年
    2020-11-21 07:24

    You can call findViewById() with the Activity Object you get inside your public void onAttach(Activity activity) method inside your Fragment.

    Save the Activity into a variable for example:

    In the Fragment class: private Activity mainActivity; In the onAttach() method: this.mainActivity=activity;

    Finally execute every findViewById through the vairable: mainActivity.findViewById(R.id.TextView);

提交回复
热议问题