java - Android ProgressDailog crashes on dismiss while in a thread -
hi trying execute code below suppose open loading dialog , dismiss in if
statement.
here code:
loginbtn.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { final progressdialog progress = progressdialog.show(thisactivity, "please wait", "loading please wait..", true); thread loginthread = new thread(new runnable() { @override public void run() { try { boolean userallowed = login.loginuser(useremail.gettext().tostring(), userpass.gettext().tostring()); if(userallowed) { progress.dismiss(); startactivity(mainpage); } else { progress.dismiss(); toast.maketext(context, "invalide email , password", toast.length_long).show(); } } catch (exception e) { toast.maketext(context, "there problem", toast.length_long).show(); } } }); loginthread.start(); } });
the logcat error output is:
05-09 22:37:26.508: e/androidruntime(24820): fatal exception: thread-1306 05-09 22:37:26.508: e/androidruntime(24820): java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() 05-09 22:37:26.508: e/androidruntime(24820): @ android.os.handler.<init>(handler.java:197) 05-09 22:37:26.508: e/androidruntime(24820): @ android.os.handler.<init>(handler.java:111) 05-09 22:37:26.508: e/androidruntime(24820): @ android.widget.toast$tn.<init>(toast.java:324) 05-09 22:37:26.508: e/androidruntime(24820): @ android.widget.toast.<init>(toast.java:91) 05-09 22:37:26.508: e/androidruntime(24820): @ android.widget.toast.maketext(toast.java:238) 05-09 22:37:26.508: e/androidruntime(24820): @ com.shale.activities.mainactivity$1$1.run(mainactivity.java:93) 05-09 22:37:26.508: e/androidruntime(24820): @ java.lang.thread.run(thread.java:856)
my reference this tutorial.
thanks!
as in log :
runtimeexception: can't create handler inside thread has not called looper.prepare()
means trying update or access ui elements non ui thread . need use activity.runonuithread , handler or asynctask
updating or accessing ui other thread. as:
your_activity.this.runonuithread(new runnable() { @override public void run() { //update ui elements here } });
Comments
Post a Comment