java - Application stops after progress bar reaching 100 -
i built small progress bar. when press button, progress bar starts counting, 1 100. changed button text "running" when runs. when tried change text "again?" after reached 100 (after while loop finished), application has stopped - every time. help? thank in advance.
package com.myfirstapplication.owner.myfirstapplication; import android.os.handler; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.progressbar; import android.widget.textclock; import android.widget.textview; public class mainactivity extends appcompatactivity { progressbar pg; button bt; int progressstatus = 0; textview tv; handler handler = new handler(); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); pg = (progressbar) findviewbyid(r.id.progressbar); bt = (button) findviewbyid(r.id.btn); tv = (textview) findviewbyid(r.id.textid); bt.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { bt.settext("running."); progressstatus = 0; new thread(new runnable() { @override public void run() { while (progressstatus < 100) { progressstatus++; handler.post(new runnable() { @override public void run() { pg.setprogress(progressstatus); tv.settext("progress: " + progressstatus + "/" + pg.getmax()); } }); try { thread.sleep(100); } catch (interruptedexception e) { e.printstacktrace(); } } bt.settext("again?"); } }).start(); } }); } }
bt.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { bt.settext("running."); progressstatus = 0; new thread(new runnable() { @override public void run() { while (progressstatus < 100) { progressstatus++; handler.post(new runnable() { @override public void run() { pg.setprogress(progressstatus); tv.settext("progress: " + progressstatus + "/" + pg.getmax()); } }); try { thread.sleep(100); } catch (interruptedexception e) { e.printstacktrace(); } } handler.post(new runnable() { @override public void run() { bt.settext("again?"); } }); } }).start(); } });
Comments
Post a Comment