android - ImageSwitcher in fragment throws illegalstateexception but works in an activity -


my code works when in activity. however, decided change fragment, , throws illegalstateexception viewswitcher. can't figure out wrong. in summary, have images use placeholders. when upload image button clicked, fetches uri returned path , uses replace placeholder image in imageswitcher.

here's code:

private button buttonupload; private imageswitcher imageswitcher; private imagebutton imagebutton0; private imagebutton imagebutton1; private imagebutton imagebutton2; private imagebutton imagebutton3; private imagebutton imagebutton4; private arraylist<string> photoarray; int counter = 0; private static int load_image_results=1; private static int placeholder_image = r.drawable.placeholder_2; int[] imgs = {r.drawable.pic1, r.drawable.pic2, r.drawable.pic3,r.drawable.pic4,r.drawable.pic5};   @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) {     // todo auto-generated method stub      view rootview = inflater.inflate(r.layout.new_item_fragment_1, container,false);     return rootview; }   public void onstart() {     // todo auto-generated method stub     super.onstart();      //initialize variables , set default photos     initvariables(); }     private void initvariables(){        //photo array     photoarray = new arraylist<string>();      buttonupload = (button) getactivity().findviewbyid(r.id.button_upload_ni);     buttonupload.setonclicklistener(this);      //image buttons     imagebutton0 = (imagebutton) getactivity().findviewbyid(r.id.imagebutton0);     imagebutton0.setimageresource(imgs[0]);     imagebutton0.setonclicklistener(this);     imagebutton1 = (imagebutton) getactivity().findviewbyid(r.id.imagebutton1);     imagebutton1.setimageresource(imgs[1]);     imagebutton1.setonclicklistener(this);     imagebutton2 = (imagebutton) getactivity().findviewbyid(r.id.imagebutton2);     imagebutton2.setonclicklistener(this);     imagebutton3 = (imagebutton) getactivity().findviewbyid(r.id.imagebutton3);     imagebutton3.setonclicklistener(this);     imagebutton4 = (imagebutton) getactivity().findviewbyid(r.id.imagebutton4);     imagebutton4.setonclicklistener(this);      imageswitcher = (imageswitcher) getactivity().findviewbyid(r.id.image_main_previewer);     imageswitcher.setfactory(this);      //add animation preview     imageswitcher.setinanimation(animationutils.loadanimation(getactivity(), r.anim.abc_fade_in));     imageswitcher.setoutanimation(animationutils.loadanimation(getactivity(), r.anim.abc_fade_out));      //set default placeholder     imageswitcher.setimageresource(placeholder_image); }  @override public void onactivityresult(int requestcode, int resultcode, intent data) {      super.onactivityresult(requestcode, resultcode, data);      system.out.println("got beginning of onact");      // here need check if activity triggers image gallery.     // if requestcode match load_image_results value.     // if resultcode result_ok , there data know image picked.     if (requestcode == load_image_results && resultcode == activity.result_ok && data != null) {         // let's read picked image data - uri         uri pickedimageuri = data.getdata();         // let's read picked image path using content resolver     //    string[] filepath = { mediastore.images.media.data };     //    cursor cursor = getcontentresolver().query(pickedimage, filepath, null, null, null);    //     cursor.movetofirst();    //     string imagepath = cursor.getstring(cursor.getcolumnindex(filepath[0]));          // need set gui imageview data data read picked file.      //   uploadview.setimagebitmap(bitmapfactory.decodefile(imagepath));         //setnewimage(pickedimageuri);          string finalpath = getfinalfilepath(pickedimageuri);         setnewimage(pickedimageuri, finalpath);          // @ end remember close cursor or end runtimeexception!  //       cursor.close();          system.out.println("got end of onact");      } }    private void setnewimage(uri uri, string filepath){     system.out.println("got beginning of setnewimage");      imageswitcher.setimageuri(uri);      //also set image thumb nail     //int buttonid = getthumbbutton(); //  imagebutton changebutton = (imagebutton) getactivity().findviewbyid(buttonid); //  changebutton.setimagebitmap(decodesampledbitmapfromfile(filepath,40,40));      //finally save bitmap     addpathtoarray(filepath);     system.out.println("got end of setnewimage"); }     private void addpathtoarray(string filepath){     //bitmap tempitemimage = bitmapfactory.decodefile(filepath);     system.out.println("entered addbit");     photoarray.add(filepath);     system.out.println("exit addbit "+photoarray.size()); }     private int getthumbbutton(){     //get size of array     int arraysize = photoarray.size();     int buttonid = 0;     system.out.println("entered getthumb");     //switch through find button change     switch(arraysize){     case 0:         buttonid = r.id.imagebutton0;         break;     case 1:         buttonid = r.id.imagebutton1;         break;     case 2:         buttonid = r.id.imagebutton2;         break;     case 3:         buttonid = r.id.imagebutton3;         break;     case 4:         buttonid = r.id.imagebutton4;         break;     default:;      }      return buttonid; }     private string getfinalfilepath(uri uri){     string[] filepath = { mediastore.images.media.data };     cursor cursor = getactivity().getcontentresolver().query(uri, filepath, null, null, null);     cursor.movetofirst();     string imagepath = cursor.getstring(cursor.getcolumnindex(filepath[0]));     cursor.close();      //now need set gui imageview data data read picked file.       return imagepath;   }     @override public void onclick(view view) {      int viewid = view.getid();      switch(viewid){     case r.id.button_upload_ni:         getimagefromgallery();         break;     case r.id.imagebutton0:         setimageatposition(0);         break;     case r.id.imagebutton1:         setimageatposition(1);         break;     case r.id.imagebutton2:         setimageatposition(2);         break;     case r.id.imagebutton3:         setimageatposition(3);         break;     case r.id.imagebutton4:         setimageatposition(4);         break;     default: //nothing here         ;     }  }     private void setimageatposition(int position){ //  imageswitcher.setimageresource(imgs[position]); //  imageswitcher.setimagedrawable(drawable.createfrompath(photoarray.get(position))); }     private void getimagefromgallery(){    // create intent image gallery.    intent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri);     // start new activity load_image_results handle results when image picked image gallery.    startactivityforresult(i, load_image_results);    }      @override public view makeview() {     // create view     system.out.println("make view called");     counter++;     system.out.println("setnewimage counter= "+counter);     imageview view = new imageview(getactivity());     view.setscaletype(imageview.scaletype.fit_center);     view.setlayoutparams(new imageswitcher.layoutparams (android.view.viewgroup.layoutparams.match_parent,android.view.viewgroup.layoutparams.match_parent));     view.setbackgroundcolor(0xff000000);     system.out.println("make view ending");     return view; }     public static bitmap decodesampledbitmapfromfile(string filepath, int reqwidth, int reqheight){      //first check dimensions injustdecodebounds=true      final bitmapfactory.options options = new bitmapfactory.options();     options.injustdecodebounds = true;     bitmapfactory.decodefile(filepath,options);      //calculate insampledsize     options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight);      //decode bitmap insamplesize set     options.injustdecodebounds = false;     return bitmapfactory.decodefile(filepath, options); }     public static int calculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight){     //raw height , width of image     final int height = options.outheight;     final int width = options.outwidth;     int insamplesize = 1;      if(height>reqheight || width > reqwidth){         final int halfheight = height/2;         final int halfwidth = width/2;          //calculate highest insamplesize value...         while((halfheight/insamplesize)>reqheight && (halfwidth/insamplesize)>reqwidth){             insamplesize *=2;         }     }     return insamplesize; } 

finally figured out. fragment's onstart() method called when activity gets focus again. therefore, views initialized again. second time, onactivityresult adds third view instead of replacing 1 of others , illegalstateexception thrown. fix it, initialized views in onactivitycreated() method instead of onstart();


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -