android - How to add ImageView to layout programmatically -


I have a problem adding the ImageView to the layout. These are my codes:

  Protected void crate (bundle saved instenstate) {super.onCreate (savedInstanceState); RequestWindowFeature (Window.FEATURE_NO_TITLE); SetContentView (R.layout.activity_main); Find Relative Layout Layout = (Relative Layout) VVBIID (RID. ImageView Photo = New ImageView (this); Picture.setImageResource (R.drawable.picture1); Layout.addView (picture); }  

There are no errors shown in Eclipse, but when I run it on my device, the program crashes.

You are getting NPE as your piece for relative layout. So please get it in onCreateView of this type of piece:

  @ override public view creatives (layoutInfleter Inflator, View Group container, bundle savedinstenstate) {see View = inflater.inflate (rl.place_holder_framement, container, false); Relative layout layout = (relative layout) view.findViewById (RID .relative); ImageView Photo = New ImageView (this); Picture.setImageResource (R.drawable.picture1); Layout.addView (picture); Return view; }  


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -