简体   繁体   中英

How to view an Image View to a linear layout in android dynamically on andorid

I have two activities. One that submits the data and one that shows the submitted data. I am able to add the images to the first activity fine and the data is then stored in a singleton data object with a ArrayList instance called "images".

The problem I have is that when trying to retrieve the images from the image array and display them it crashes the app. I have tried to debug the code and it runs perfectly until you get to the last line:

attachmentLayout.addView(image,imParams);

The Images are stored in the singleton correctly and when you debug it, it shows that they are in fact there.

I tried to figure it out for myself, but I can't find what it is. I am quite new to android programming so help would be much appreciated. I have added my code that tries to get the images below.

if(values.getImage()!=null){

        int count = images.size()-1;
        for (int x=0;x<=count;x++){
            //declare attachments and get image from arraylist<ImageView>
            LinearLayout attachmentLayout = (LinearLayout) findViewById(R.id.attachments);
            ImageView image;
            image = images.get(x);
            //set parameters
            LinearLayout.LayoutParams imParams =
                    new LinearLayout.LayoutParams(90,270);
            image.setBottom(attachmentLayout.getBottom());
            image.setVisibility(View.VISIBLE);
            image.isShown();
            //add to linear layout
            attachmentLayout.addView(image,imParams);

        }
    }

Edit: This is the logcat I get when the app restarts after is crashes. I don't believe this is helpful though.

08-24 12:34:18.522    4782-4820/com.example.jules_gribble.test I/Adreno﹕ QUALCOMM build                   : 065751b,
Build Date                       : 04/15/15
OpenGL ES Shader Compiler Version: E031.25.03.07
Local Branch                     :
Remote Branch                    : quic/LA.BF64.1.2.1_rb2.9
Remote Branch                    : NONE
Reconstruct Branch               : AU_LINUX_ANDROID_LA.BF64.1.2.1_RB2.05.01.00.081.016 + 065751b +  NOTHING

Managed to answer my question myself. Turns out it was crashing because I wa overlooking that in the submit activity it was declared

ImageView image = new ImageView(Test.this);

This meant it could not be used in a different activity (Test is the name of the class of the submit activity which will be changed).

To solve this I changed it to

ImageView image = new ImageView(Submitted.this);

where submitted is the name of the submitted activity. I then needed to get the bitmap of the image and set the bitmap to the new imageview.

Bitmap bitmap = ((BitmapDrawable)images.get(x).getDrawable()).getBitmap();
image.setImageBitmap(bitmap);

I found this from HERE .

Hope this will help someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM