java - TextView giving NulllPointerException in AsyncTask -
that should trivial , wondering whats happening, copied code online course, worked , , giving me nullpointer exception.everything seems right far
here mainactivity class
public class mainactivity extends activity { button button1; textview textview2; progressbar progressbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview textview2=(textview)findviewbyid(r.id.textview2); button button1=(button)findviewbyid(r.id.button1); progressbar progressbar1=(progressbar)findviewbyid(r.id.progressbar1); button1.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { asy task=new asy(mainactivity.this); task.execute("process me","process me too"); } }); }
}
then thats sniplet async
public class asy extends asynctask<string,integer,long> { public mainactivity host; public asy (mainactivity host) { this.host=host; } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); host.textview2.settext("processing.."); ; }
and in logcat see
e/androidruntime(28044): fatal exception: main e/androidruntime(28044): java.lang.nullpointerexception e/androidruntime(28044): @ com.example.async.asy.onpreexecute(asy.java:25) e/androidruntime(28044): @ android.os.asynctask.execute(asynctask.java:391)
i mean looks right me , initialize textview after setcontentview, use constructors, why not initialized in asynctask ?
declare textview instance global , use in asynctask.
like..
textview textview2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview2=(textview)findviewbyid(r.id.textview2);
and use :
textview2.settext("processing..");
Comments
Post a Comment