findViewById in Fragment

后端 未结 30 2268
终归单人心
终归单人心 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:35

    getView() works only after onCreateView() completed, so invoke it from onPostCreate():

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        ImageView imageView = (ImageView) getView().findViewById(R.id.foo);
    }
    

提交回复
热议问题