findViewById in Fragment

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

    Try this it works for me

    public class TestClass extends Fragment {
        private ImageView imageView;
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.testclassfragment, container, false);
            findViews(view);
            return view;
        }
    
        private void findViews(View view) {
            imageView = (ImageView) view.findViewById(R.id.my_image);
        }
    }
    

提交回复
热议问题