java - App has stopped - ProgressBar in Android -
friends. started learn android app developinng android studio , java. trying make 1 progressbar. when app started have 2 edittext fields first 100 default(how max value progressbar ) , 1 edittext field increment by(this step) progressbar. when click start must show via dialog.
i write code, there no more errors, app closing when hit start button. progressbar not working
this code: activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:id="@+id/txt_max" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="max value" /> <edittext android:id="@+id/maximum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="100.0" /> <textview android:id="@+id/txt_increment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="increment by"/> <edittext android:id="@+id/increment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5.0" /> <button android:id="@+id/butt_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" /> </linearlayout> this mainactivity.java:
public class mainactivity extends appcompatactivity { int increment; progressdialog dialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button startbutton = (button) findviewbyid(r.id.butt_start); startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { edittext et = (edittext) findviewbyid(increment); increment = integer.parseint(et.gettext().tostring()); dialog = new progressdialog(mainactivity.this); dialog.setcancelable(true); dialog.setmessage("loading..."); dialog.setprogressstyle(progressdialog.style_horizontal); dialog.setprogress(0); edittext max = (edittext) findviewbyid(r.id.maximum); int maximum = integer.parseint(max.gettext().tostring()); dialog.setmax(maximum); dialog.show(); thread background = new thread(new runnable() { public void run() { try { while (dialog.getprogress() <= dialog.getmax()) { thread.sleep(500); progresshandler.sendmessage(progresshandler.obtainmessage()); } } catch (java.lang.interruptedexception e) { } } }); background.start(); } handler progresshandler = new handler() { public void handlemessage(message msg) { dialog.incrementprogressby(increment); } }; }); } }
there error code.. comment each reporting snippet:
public class mainactivity extends appcompatactivity { int increment; progressdialog dialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button startbutton = (button) findviewbyid(r.id.butt_start); startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { edittext et = (edittext) findviewbyid(increment); here first error: not wrong thing, it's better istantiate edittext in oncreate (outside onclick), because way re-create edittext every time click button.
the error double: using findviewbyid(increment) where, in code, increment int variable value 0. have use (as did button)
findviewbyid(r.id.increment); going on:
increment = integer.parseint(et.gettext().tostring()); dialog = new progressdialog(mainactivity.this); dialog.setcancelable(true); dialog.setmessage("loading..."); dialog.setprogressstyle(progressdialog.style_horizontal); dialog.setprogress(0); edittext max = (edittext) findviewbyid(r.id.maximum); int maximum = integer.parseint(max.gettext().tostring()); here error xml side: since assuming max.gettext().tostring() integer, add inputtype xml number value.
dialog.setmax(maximum); dialog.show(); thread background = new thread(new runnable() { public void run() { try { while (dialog.getprogress() <= dialog.getmax()) { thread.sleep(500); progresshandler.sendmessage(progresshandler.obtainmessage()); } } catch (java.lang.interruptedexception e) { } } }); background.start(); } handler progresshandler = new handler() { public void handlemessage(message msg) { dialog.incrementprogressby(increment); } }; }); } } another thing is better avoid naming variable same name of id, expecially because id should representative of control pointing @ (for example call "edittextmaxvaluemainactivity", limit example representative).
if can suggest you, thenewboston channel/site best tutorials, has couple of playlist android basics advanced , simple , explicative. luck!
for other errors, need logtrace
Comments
Post a Comment