0

I am trying to build an app that uses the device's camera, takes a picture in one fragment, and displays that picture in a separate fragment. My app uses a DrawerLayout and several fragments. In the main Activity class when I try to reference the ImageView I would like to edit within the xml file of one of my fragments I am met with a NPE:

ImageView imgTaken = (ImageView) findViewById(R.id.post_imageView);

I have been encountering these types of exceptions too frequently; I think there is something about using View objects that I am not familiar with. If anybody has any useful insight on why exactly I may be repeatedly finding my code not working due to a NPE, please feel free to enlighten me.

Thanks for the help!

McFizz
  • 3,013
  • 3
  • 18
  • 26
  • do u make sure that the layout file contain the post_imageView id? – herokingsley Jan 17 '16 at 03:48
  • @herokingsley yes, everything is there; that's why I'm confused that this wont work – McFizz Jan 17 '16 at 03:53
  • Please post some more code and xml. The current information is not sufficient to answer. – Sandeep Kaul Jan 17 '16 at 03:55
  • 1
    If the ImageView is in one of your fragment layouts, and the fragment has not yet been added to the activity, then `findViewById` will return null because it the view is not in the view hierarchy yet. Where are you adding the fragment to the activity (or are you adding the fragment directly in the XML layout for the activity)? – Karakuri Jan 17 '16 at 04:00
  • do you have any id for your xml layout???? If have then reference it and call as by that reference your image view eg in your activity nameofyourlayoutid = mylayout; and reference as ImageView imgTaken = (ImageView) mylayout.findViewById(R.id.post_imageView); – Misbah Farooqi Jan 17 '16 at 04:31
  • @Karakuri my app contains only two activities: an Login screen and a Navigation Drawer containing several fragments. After leaving the Login screen and calling the Nav Drawer, all fragments would be added, correct? – McFizz Jan 17 '16 at 22:23

0 Answers0