简体   繁体   中英

When I try to add more than 4 images to the layout, the app crashes

I am making a simple table layout and displaying two images per row. When I add four images that fits in a single screen than its working correct but when I add more rows and other images the app crashes and says the app has stopped working. I have yet not started touching the mainActivity.class file. Just making a layout is giving me this error!

Here is the xml code:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <TableRow>
            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView1"
                android:layout_row="0"
                android:layout_column="0"
                android:src="@drawable/img1"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView2"
                android:layout_row="0"
                android:layout_column="1"
                android:src="@drawable/img2"/>
        </TableRow>
        <TableRow>
            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView3"
                android:layout_row="1"
                android:layout_column="0"
                android:src="@drawable/img3"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView4"
                android:layout_row="1"
                android:layout_column="1"
                android:src="@drawable/img4"/>
        </TableRow>
        <TableRow>
            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView5"
                android:layout_row="2"
                android:layout_column="0"
                android:src="@drawable/img5"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView6"
                android:layout_row="2"
                android:layout_column="1"
                android:src="@drawable/img6"/>
        </TableRow>
        <TableRow>
            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView7"
                android:layout_row="3"
                android:layout_column="0"
                android:src="@drawable/img7"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView8"
                android:layout_row="3"
                android:layout_column="1"
                android:src="@drawable/img8"/>
        </TableRow>
        <TableRow>
            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView9"
                android:layout_row="4"
                android:layout_column="0"
                android:src="@drawable/img9"/>

            <ImageView
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:id="@+id/imageView10"
                android:layout_row="4"
                android:layout_column="1"
                android:src="@drawable/img10"/>
        </TableRow>

    </TableLayout>
</RelativeLayout>

Error Log:

06-21 13:42:16.190 1542-1551/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called at dalvik.system.CloseGuard.open(CloseGuard.java:184) at android.os.ParcelFileDescriptor.

You have to use libraries for that purpose because dealing with memory is hard task when adding images to your application and you don't want to do it by yourself.

Thankfully, Picasso Library is very good for that and you should better read about it and use it in your application.

Here is a link to the library:

square/picassor . GitHub

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