android - When creating a button from a Dialog Box -
the original code has been deleted, new working code shown. idea behind code create new textview within layout has custom name user provides. previously, npe error happening. fix. questions, please feel free ask.
edit: found solution
the fix needs followed:
accountedit = new edittext(this); // accountedit needs global variable
then within builder.setpositivebutton
builder.setpositivebutton(r.string.btn_save, new dialoginterface.onclicklistener(){ public void onclick(dialoginterface dinterface, int whichbutton) { linearlayout linelayout = (linearlayout)findviewbyid(r.id.linear_layout); string newaccountname = accountedit.gettext().tostring(); newtextview = new textview( getbasecontext() ); newtextview.setvisibility(view.visible); newtextview.settext( newaccountname ); newtextview.setid(id); newtextview.settextsize(35); newtextview.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { onclicknew(view); } }); newtextview.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view view) { toast.maketext(getbasecontext(), "testing" , toast.length_long).show(); return true; } });
this create button, set name of button information in edittext within dialog box. previously, edittext activity, , being called wrong, caused npe. thank help.
as wenhui metioned, call finviewbyid inside onclick listener of button, wrong context used. in following example:
final edittext accountedit = (edittext)findviewbyid(r.id.newaccountbutton); final string newaccountname = accountedit.gettext().tostring(); final linearlayout linelayout = (linearlayout)findviewbyid(r.id.linear_layout); builder.setpositivebutton(r.string.btn_save, new dialoginterface.onclicklistener(){ public void onclick(dialoginterface dinterface, int whichbutton) { newtextview = new textview(getbasecontext()); newtextview.setvisibility(view.visible); newtextview.settext("test"); newtextview.setid(id); newtextview.settextsize(35); newtextview.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { onclicknew(view); } }); linelayout.addview(newtextview); id++; } });
Comments
Post a Comment