java - How to Execute AsyncTask in Android - erro in class Zygotelnit -
i have class extends asynctask receive context, string user , string password,
i try execute asynctask, not working.
when execute asynctask class, occurs problem in class zygotelnit
public void run() { try { mmethod.invoke(null, new object[] { margs }); } catch (illegalaccessexception ex) { throw new runtimeexception(ex); } catch (invocationtargetexception ex) { throwable cause = ex.getcause(); if (cause instanceof runtimeexception) { throw (runtimeexception) cause; } else if (cause instanceof error) { throw (error) cause; } throw new runtimeexception(ex); } }
follow below class execute asynctask
mbuttonenter.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { username = medittextuser.gettext().tostring(); password = medittextpassword.gettext().tostring(); new loginservice(loginactivity.this, username, password).execute();
follow below class asyntask
public class loginservice extends asynctask<string, string, string> { private string username; private string password; private context context; progressdialog progressdialog = new progressdialog(context); public loginservice(context context, string username, string password){ this.context = context; this.username = username; this.password = password; } @override protected string doinbackground(string... params) { string result = ""; try { url url = new url("http://192.168.0.11:8080/appoint-api/api/usuario/dologin?user=" + this.username + "&senha=" + this.password); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("post"); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(httpurlconnection.getinputstream())); string inputline; stringbuilder response = new stringbuilder(); while ((inputline = bufferedreader.readline()) != null) { response.append(inputline); } result = response.tostring(); bufferedreader.close(); } catch (exception e) { log.d("inputstream", e.getmessage()); } return result; } }
follow error in log cat
08-16 22:23:29.781 18099-18099/br.com.appoint.android d/androidruntime﹕ shutting down vm 08-16 22:23:29.781 18099-18099/br.com.appoint.android w/dalvikvm﹕ threadid=1: thread exiting uncaught exception (group=0x4108b960) 08-16 22:23:29.875 18099-18099/br.com.appoint.android e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ android.app.alertdialog.resolvedialogtheme(alertdialog.java:143) @ android.app.alertdialog.<init>(alertdialog.java:98) @ android.app.progressdialog.<init>(progressdialog.java:77) @ br.com.appoint.android.service.loginservice.<init>(loginservice.java:25) @ br.com.appoint.android.activity.loginactivity$1.onclick(loginactivity.java:63) @ android.view.view.performclick(view.java:4452) @ android.widget.button.performclick(button.java:148) @ android.view.view$performclick.run(view.java:18428) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:176) @ android.app.activitythread.main(activitythread.java:5365) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) @ dalvik.system.nativestart.main(native method)
new progressdialog(context);
executed before public loginservice(context context, string username, string password)
, means when progressdialog
initialized, context
still null
, cause npe
.
remove progressdialog progressdialog = new progressdialog(context);
or initialze progressdialog
in public loginservice(context context, string username, string password)
Comments
Post a Comment