How to use Gallery View in Android, but on another page -
i've got 2 layout files in res/layout folder: main.xml , page2.xml. in main.xml i've got welcome info , button starts
setcontentview(r.layout.page2); to change page2.xml.
it worked fine till decided add gallery view in page2.xml.
when set begining contentview page2 below it's ok.
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.page2); gallery g = (gallery) findviewbyid(r.id.gallery); g.setadapter(new imageadapter(this)); but when call main.xml first show may start page...
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); gallery g = (gallery) findviewbyid(r.id.gallery); g.setadapter(new imageadapter(this)); the app returns error. know problem context in line
g.setadapter(new imageadapter(this)); but don't know how pass right context or solve in way (but don't want have layout in 1 xml file).
it's not entirely clear explanation (logs never hurt), think you're getting null pointer exception because gallery not defined in main.xml. there 2 solutions problem:
- split 2 "pages" 2 activities. more natural way of dealing things. try rotating phone/emulator , you'll see mean. when user clicks on button or whatever, call
startactivity(), callfinish(), welcome activity not left hanging about. - the problem
findviewbyid()acts on whatever "visible" in activity right now. since didsetcontentview(main),gallerynot there. try "getting" gallery after make call "change pages" (setcontentview(r.layout.page2);).
however, advice go first option.
Comments
Post a Comment