0

I am new to android programming. Can someone help me out? I am testing how to display images and string in a list view using arrayadapter. I even tried base adapter in a different method but same issue, the app crashes and never runs.

Here is the code for my MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity {
    ListView l;
    int [] Images = {R.drawable.img1,R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10};
    String [] Titles;
    String [] Description;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Resources resources=getResources();
        Titles = resources.getStringArray(R.array.Title);
        Description = resources.getStringArray(R.array.Description);
        l = findViewById(R.id.listView);
        PopulateRows arrayAdapter = new PopulateRows(this, Titles,Images, Description);
        l.setAdapter(arrayAdapter);

    }
    class PopulateRows extends ArrayAdapter<String>{
        Context context;
        int [] Images;
        String[] Titles;
        String [] Description;
        PopulateRows(Context context, String[] Titles, int[] imgs, String[] Description){
            super(context,R.layout.custom_layout, R.id.textView, Titles);
            this.context=context;
            this.Images=imgs;
            this.Titles=Titles;
            this.Description=Description;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.custom_layout,parent,false);

            ImageView imageView = row.findViewById(R.id.imageView);
            TextView textView =  row.findViewById(R.id.textView);
            TextView textView1 = row.findViewById(R.id.textView1);

            imageView.setImageResource(Images[position]);
            textView.setText(Titles[position]);
            textView1.setText(Description[position]);
            return row;
        }
    }
}

And here is the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.name.mypackage.MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ListView>

</android.support.constraint.ConstraintLayout>

and again here is the other custom_layout.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_marginStart="28dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="29dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="49dp"
        android:text="TextView"
        android:layout_alignTop="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignStart="@+id/textView"
        android:text="TextView" />

</RelativeLayout>

When I run this code, I'll always get the following error and the app crashes:

--------- beginning of crash 09-12 10:11:44.925 4706-4706/com.example.name.mypackageE/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.name.mypackage, PID: 4706 java.lang.OutOfMemoryError: Failed to allocate a 286654476 byte allocation with 4896858 free bytes and 248MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) at android.content.res.Resources.loadDrawableForCookie(Resources.java:2638) at android.content.res.Resources.loadDrawable(Resources.java:2543) at android.content.res.Resources.getDrawable(Resources.java:806) at android.content.Context.getDrawable(Context.java:458) at android.widget.ImageView.resolveUri(ImageView.java:826) at android.widget.ImageView.setImageResource(ImageView.java:433) at com.example.name.mypackage.MainActivity$PopulateRows.getView(MainActivity.java:55) at android.widget.AbsListView.obtainView(AbsListView.java:2346) at android.widget.ListView.measureHeightOfChildren(ListView.java:1281) at android.widget.ListView.onMeasure(ListView.java:1188) at android.view.View.measure(View.java:18809) at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973) at android.view.View.measure(View.java:18809) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:18809) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) at android.view.View.measure(View.java:18809) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) at android.view.View.measure(View.java:18809) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1228) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:746) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5443) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

code that makes list appear without images, only displays string arrays

public class MainActivity extends Activity {
    ListView l;
    //int [] Images = {R.drawable.img1,R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10};
    String [] Titles;
    String [] Description;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Resources resources=getResources();
        Titles = resources.getStringArray(R.array.Title);
        Description = resources.getStringArray(R.array.Description);
        l = findViewById(R.id.listView);
        PopulateRows arrayAdapter = new PopulateRows(this, Titles, Description);
        l.setAdapter(arrayAdapter);

    }
    class PopulateRows extends ArrayAdapter<String>{
        Context context;
        //int [] Images;
        String[] Titles;
        String [] Description;
        PopulateRows(Context context, String[] Titles, String[] Description){
            super(context,R.layout.custom_layout, R.id.textView, Titles);
            this.context=context;
            //this.Images=imgs;
            this.Titles=Titles;
            this.Description=Description;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.custom_layout,parent,false);

            //ImageView imageView = row.findViewById(R.id.imageView);
            TextView textView =  row.findViewById(R.id.textView);
            TextView textView1 = row.findViewById(R.id.textView1);

            //imageView.setImageResource(Images[position]);
            textView.setText(Titles[position]);
            textView1.setText(Description[position]);
            return row;
        }
    }
}
  • The error says that whatever your loading into your imageview is too large, causing you to have a memory overflow. Unfortunately I am not poised to point you in the right direction as I am not experienced with that. I do notice that you have the src of the imageview pointed to your mipmap directory, yet you've got your images in your drawable folder. You might check to see if the correct image is being pulled from the array you have, based on the location being given. If you get rid of the ImageView, does ListView work as expected? – miversen33 Sep 12 '17 at 04:51
  • check: https://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio – Nguyễn Trung Hiếu Sep 12 '17 at 04:54
  • Drawable images are causing problem – Sharath kumar Sep 12 '17 at 04:54
  • Thanks for responding so fast, i will check, what you have suggested and get back soon – Judah Irani Sep 12 '17 at 04:55
  • 2
    If your images are high resolutions images then load your images using _Glide_ ,_Picasso_ or _UIL_ library. – Piyush Sep 12 '17 at 05:01
  • @T-he-game Did as you said and i got the string array and app worked, thanks, but what do i do about the images, posting code that worked – Judah Irani Sep 12 '17 at 05:09
  • I am unsure what your images are but as others have said, this tends to happen when using a drawable. you might consider just importing the image into your project via the "Import Image Asset" option in AS, as it will scale things down as needed, then reference it via R.mipmap.yourimage instead. That or you will have to manually downscale everything and place in the appropriate drawable directories. – miversen33 Sep 12 '17 at 05:14
  • It worked using lower resolution of images – Judah Irani Sep 12 '17 at 05:28

2 Answers2

1

Java out of memory error means the resolution of your images is very high. Your view doesn't get populated thats why you are receiving crashes.

Adjust your image resolution by making drawable for different screen resolution drawable--hdpi, drawable--xhdpi, drawable--xxhdpi, drawable--xxxhdpi.

Instead of listview try using recycleview if your image content is more.

garry
  • 419
  • 5
  • 10
0

try to use Images that having minimum size your code crashes because of Images that you are using , for more you may use

android:hardwareAccelerated="false" 
android:largeHeap="true" 

in android manifest like below

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

go through this link for more ....

Omkar
  • 3,040
  • 1
  • 22
  • 42
  • This is a tweak, although seems good on purpose but extremely degrades performance of application. – garry Sep 12 '17 at 05:05