android - ViewFlipper.addView() gives Exception, while Showing Progress Dialog in AsyncTask -


what want: want show progress dialog while adding views dynamically viewflipper.

what have: have used asynctask achieve this. viewflipper declared in main activity, adding views viewflipper in doinbackground() of asynctask.

what problem: getting exception on viewflipper.addview() statement , exception " main has leaked window com.android.internal.policy.impl ..... added here. " , this.

here code:

public class main extends activity {        private viewflipper viewflipper;     /** called when activity first created. */    @override    public void oncreate(bundle savedinstancestate)    {       super.oncreate(savedinstancestate);       setcontentview(r.layout.main_flipper);        viewflipper = (viewflipper)findviewbyid(r.id.id_vf_main);       new loaddata().execute();       // other tasks.    }      class loaddata extends asynctask<object, void, string>    {       protected progressdialog  progressdialog;        @override       protected void onpreexecute()       {           super.onpreexecute();                    progressdialog = progressdialog.show(main.this,"loading", "loading data...", true, false);       }        @override       protected string doinbackground(object... parametros)       {           (int = 0; < login.data_channel_name.size(); i++)           {             layoutinflater inflater = getlayoutinflater();             relativelayout rl_main = (relativelayout) inflater.inflate(r.layout.main,null);               textview tv_channelnumber = (textview)rl_main.findviewbyid(r.id.id_tv_channelnumber);             if(tv_channelnumber != null)             {                 tv_channelnumber.settext("some number");             }              textview tv_channelname = (textview)rl_main.findviewbyid(r.id.id_tv_channelname);             if(tv_channelname != null)             {                 tv_channelname.settext("some name");             }              viewflipper.addview(rl_main);           }                return null;       }        @override       protected void onpostexecute(string result)       {           super.onpostexecute(result);           progressdialog.dismiss();       }   } } 

you trying interact ui background thread isn't allowed. allowed mess ui in onpostexecute() , onpreexecute() methods. there method; onprogressupdate() can use so:

protected void onprogressupdate(integer... progress) { // on ui thread!     viewflipper.addview(rl_main); } 

obviously you'll need handle passing view method creating field or something. can call within doinbackground() following:

publishprogress(0); 

the function typically used updating percentage on loading bar or similar (hence passing integer) should work fine your purposes.


Comments

Popular posts from this blog

jQuery Ajax Render Fragments OR Whole Page -

javascript - Iterate over array and calculate average values of array-parts -

ASP.NET Javascript: window.open won't work twice -