textview - What's wrong with this Android program? -



textview - What's wrong with this Android program? -

i writing test application consists of: 2 buttons 1 edittext 1 textview

the first button "random" writes random name both in edittext box , textview (i have class called randomname returns string random name)

the sec button "print" writes whatver in edittext textview

the programme crashes when run , can't figure out why. help appreciated

layout image here: http://img824.imageshack.us/img824/3046/rndname.jpg

public class randomnametesteactivity extends activity implements onclicklistener { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } public edittext nomeedt = (edittext)findviewbyid(r.id.edittext1); public string nomestr = nomeedt.tostring(); public textview nometest = (textview) findviewbyid(r.id.textview1); public void onclick(view v) { // todo auto-generated method stub switch(v.getid()) { case r.id.button1: //put random name in edittext box , in textview randomname rndname = new randomname(); string rndnamestr = rndname.getname(); nometest.settext(rndnamestr); break; case r.id.button2: //print whatever in edittext box textview nometest.settext(nomestr); break; } }

}

edit: changed code following: (it doesnt crash anymore doesnt work, in buttons nothing)

public class randomnametesteactivity extends activity implements onclicklistener { /** called when activity first created. */ edittext nomeedt; textview nometest; string nomestr; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); nomeedt = (edittext)findviewbyid(r.id.edittext1); nomestr = nomeedt.tostring(); nometest = (textview) findviewbyid(r.id.textview1); } public void onclick(view v) { // todo auto-generated method stub switch(v.getid()) { case r.id.button1: //put random name in edittext box , in textview randomname rndname = new randomname(); string rndnamestr = rndname.getname(); nometest.settext(rndnamestr); break; case r.id.button2: //print whatever in edittext box textview nometest.settext(nomestr); break; } }

}

i believe issue next lines:

public edittext nomeedt = (edittext)findviewbyid(r.id.edittext1); public string nomestr = nomeedt.tostring(); public textview nometest = (textview) findviewbyid(r.id.textview1);

change to

public edittext nomeedt; public textview nometest; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); nomeedt = (edittext)findviewbyid(r.id.edittext1); nomestr = nomeedt.tostring(); nometest = (textview) findviewbyid(r.id.textview1); }

try , see if fixes issue. can't other views until have set contentview.

also helps if post stacktrace when asking question

android textview android-edittext

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -