1

I want to load all the images placed into assets subfolder in imageview and then add to ViewFlipper . It worked using drawable resource, but I need to locate the images in assets subfolders. This is a part of my code. The Android Studio don't show any error but the result only show the last image of the image list. When I uncomment the myViewFlipper.addView(imageView) line located into for loop and comment the last myViewFlipper.addView, then applicatios is crash. Sorry for my bad english. Thank you for help.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photos_layout);


        myViewFlipper = (ViewFlipper) findViewById(R.id.myflipper);
        ImageView imageView = new ImageView(PhotosActivity.this);

        final AssetManager am;
        am=getAssets();
        InputStream inputStream = null ;

        try {

            String[] images = am.list("hotel/vedado");

           for (int i = 0; i < images.length; i++) {


               try {

                   inputStream = am.open("hotel/vedado/vedado"+i+".jpg");
                   Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                   imageView.setImageBitmap(bitmap);
                   // Drawable drawable = Drawable.createFromStream(inputStream, null);
                   //imageView.setImageDrawable(drawable);
                   //myViewFlipper.addView(imageView);

               }
               catch (IOException e) {
                   e.printStackTrace();
               }


           }
           myViewFlipper.addView(imageView);

        }
        catch (IOException e) {
            e.printStackTrace();
        }



}
@Override
public boolean onTouchEvent(MotionEvent event) {
     switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            initialXPoint = event.getX();
            break;
        case MotionEvent.ACTION_UP:
            float finalx = event.getX();
            if (initialXPoint > finalx) {
                if (myViewFlipper.getDisplayedChild() == 4)
                    break;
                myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
                myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_left));
                myViewFlipper.showNext();
            } else {
               // if (myViewFlipper.getDisplayedChild() == 0)
                 //   break;
                myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));
                myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_right));
                myViewFlipper.showPrevious();
            }
            break;
    }
    return true;
}
public void to_left(View v) {
    myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
    myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_left));
    myViewFlipper.showNext();
}
public void to_right(View v) {
    myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));
    myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_right));
    myViewFlipper.showPrevious();
Rigo
  • 11
  • 2
  • 1
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Mar 13 '16 at 21:53
  • 1
    Thanks. There is a LogCat error. E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: cu.gdc.mappinremoving, PID: 2450 java.lang.RuntimeException: Unable to start activity ComponentInfo{cu.gdc.mappinremoving/cu.gdc.mappinremoving.PhotosActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. – Rigo Mar 13 '16 at 23:29

1 Answers1

1

You are only creating one ImageView. You are then trying to add that ImageView multiple times to the ViewFlipper. This will not work.

If you want multiple images in a ViewFlipper, you either need one ImageView per image, or to switch AdapterViewFlipper (so you can recycle ImageView widgets and save a lot on memory).

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • 1
    Thanks @CommonsWare. How can I create ImageView array and then addView of Images to ViewFlipper. Please put example code. – Rigo Mar 14 '16 at 01:11