android - displaying progress dialog inside runOnUiThread in fragment -
i have simple android application should display progress dialog when press 'activate' button. basically, when press 'activate', app display progress dialog while detecting nearby sensors. update progress dialog message while detects sensors. following code implemented inside fragment.
but code not display progress dialog. don't see when press 'button'. seems frozen.
@override public void onclick( view v) { v.setenabled(false); activatebutton.setenabled(true); final activity activity = getactivity(); final progressdialog pd = new progressdialog(activity); activity.runonuithread(new runnable() { @override public void run() { pd.settitle("activating..."); pd.setmessage("please wait..."); pd.setcancelable(false); pd.setindeterminate(false); pd.show(); int count = 1; pd.setmessage("detecting sensors...\n"); while (true) { if (count >= 10) { break; } count++; try { pd.setmessage("detected " + count + "=" + " sensor. \n"); thread.sleep(1000 * 1); } catch (interruptedexception e) { e.printstacktrace(); } } pd.dismiss(); } });
based on comment, changed this.
asynctask<void, void, void> task = new asynctask<void, void, void>() { progressdialog pd; @override protected void onpreexecute() { pd = new progressdialog(getactivity()); pd.settitle("activating..."); pd.setmessage("please wait..."); pd.setcancelable(false); pd.setindeterminate(false); pd.show(); } @override protected void doinbackground(void... arg0) { int count = 1; pd.setmessage("detecting sensors...\n"); try { thread.sleep(1000 * 1); } catch (interruptedexception e1) { // } stringbuffer txt = new stringbuffer(); txt.append("....."); while (true) { if (count >= 10) { break; } count++; pd.setmessage("detected " + count + "=" + " sensor. \n"); } return null; } @override protected void onpostexecute(void result) { if (pd != null) { pd.dismiss(); activatebutton.setenabled(true); } } }; task.execute((void[]) null);
but when code runs, error in line (pd.setmessage("detected " + count + "=" + " sensor. \n");).
caused by: android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views.
it helpful if can point goes wrong.
thanks.
you updating view background thread. method 'doinbackground' should call 'publishprogress' mehtod update progress dialog in 'onprogressupdate' (which execute in ui thread). check code below:
new asynctask<void, string, void>() { public progressdialog mprogress; @override protected void onpreexecute() { mprogress = progressdialog.show(getcontext(), "progress", "please wait", true); } @override protected void doinbackground(void... params) { try { thread.sleep(1000); publishprogress("progress 1"); thread.sleep(2000); publishprogress("progress 2"); thread.sleep(2000); } catch (interruptedexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void avoid) { mprogress.dismiss(); } @override protected void onprogressupdate(string... values) { mprogress.setmessage(values[0]); } };
Comments
Post a Comment