2

I have made an application with a listView, the listView contains text and pictures using an array. I alos have an editText where the user can search through the list using a search functioanlity. The listView loads in fine however when I click on the editView to search through my listView I get an outOfMemoryError. I'm not sure if this has something to do with the size of the images or something. I know questions like this have been asked before however none of them fixed my problem.

My Code is as fallows

Insurance.java

public class Insurance extends AppCompatActivity {
    ListView list;
    ArrayAdapter<String> newadapter;
    EditText inputSearch;
    String[] itemname ={
            "AA",
            "Acorn",
            "Admiral",
            "AIG",
            "Allianz",
            "Auto Direct",
            "Aviva",
            "Carrot",


    };

    Integer[] imgid= {
            R.drawable.aa,
            R.drawable.acorn,
            R.drawable.admiral,
            R.drawable.aig,
            R.drawable.allianz,
            R.drawable.autodirect,
            R.drawable.aviva,
            R.drawable.carrot,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insurance);

        CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
        list=(ListView)findViewById(R.id.list);
        newadapter = new ArrayAdapter<String>(this, R.layout.mylist, R.id.item, itemname);
        inputSearch = (EditText) findViewById(R.id.editText);
        list.setAdapter(newadapter);
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                Insurance.this.newadapter.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                String Slecteditem = itemname[+position];
                Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
                if (Slecteditem.toString() == "AA") {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:028 9032 2232"));
                    startActivity(intent);
                }
                if (Slecteditem.toString() == "Carrot") {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:028 9032 2265"));
                    startActivity(intent);
                }
            }
        });

Insurance.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/bg"
    tools:context="saveourcar.soc.Insurance">

    <EditText
        android:layout_width="400dp"
        android:layout_height="60dp"
        android:id="@+id/editText"
        android:layout_marginBottom="50dp"

/>
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="107dp">
    </ListView>



</RelativeLayout>

myList.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="10"
    android:background="#26BFDA">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="2"

        >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:padding="5dp"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="6"

        android:orientation="vertical"

        >

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF"

            />


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="TextView"
            />


    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"

        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="20dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/nextarrow"
            />
    </LinearLayout>
</LinearLayout>

I have also included android:largeHeap="true" in my Manifest file.

Logcat

04-15 17:46:49.667 27237-27237/saveourcar.soc E/AndroidRuntime: FATAL EXCEPTION: main
Process: saveourcar.soc, PID: 27237
java.lang.OutOfMemoryError: Failed to allocate a 36000012 byte allocation with 16777216 free bytes and 21MB 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:973)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2453)
at android.content.res.Resources.loadDrawable(Resources.java:2360)
at android.content.res.Resources.getDrawable(Resources.java:768)
at android.content.Context.getDrawable(Context.java:402)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:168)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
at saveourcar.soc.CustomListAdapter.getView(CustomListAdapter.java:39)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
at android.widget.ListView.onMeasure(ListView.java:1182)
at android.view.View.measure(View.java:17442)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17442)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17442)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1767)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.ru
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • `I'm not sure if this has something to do with the size of the images` It probably does - at least list out the sizes (width x height x color depth) of the images you are using. – Morrison Chang Apr 15 '16 at 16:54
  • Perhaps help you : https://stackoverflow.com/a/32245018/4813855 –  Oct 31 '17 at 09:37

1 Answers1

2
java.lang.OutOfMemoryError: Failed to allocate a 36000012 byte allocation with 16777216 free bytes and 21MB until OOM
    ...
    at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
    at saveourcar.soc.CustomListAdapter.getView(CustomListAdapter.java:39)

Your CustomListAdapter is putting a drawable resource in an ImageView. This drawable resource, when decoded and scaled for density, is 36000012 bytes. This is equivalent to a 3000px by 3000px image.

This is much too big. Find out what this resource is and fix it:

I have also included android:largeHeap="true" in my Manifest file.

Please undo that.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for getting back to me. Would it be more than one drawable image causing the problem or is it all the images collectively causing the problem – Craig Gallagher Apr 15 '16 at 17:33
  • @CraigGallagher: Well, at least one image is too large, triggering the 36000012 byte allocation. Whether you have other too-large images, I cannot say. As you can see from your error message, at the point of the crash, you could allocate at most 16777216 bytes. In reality, your heap will be fragmented, and so you will be lucky to have one contiguous free block about a tenth that size. – CommonsWare Apr 15 '16 at 17:39
  • So if I make the images a smaller resolution it should work ?? I thought Android Studio would have a way of making the resolution sizes much smaller. I find it strange how I can load all the images in the activity with the listView without a bother I only get the error once I try and filter the listView – Craig Gallagher Apr 15 '16 at 17:45
  • @CraigGallagher: "So if I make the images a smaller resolution it should work ?" -- if you make the images smaller, you will avoid this particular `OutOfMemoryError`. – CommonsWare Apr 15 '16 at 17:48