15

My code is showing this warning message:

Typed Array should be recycled after use with #recycle() for obtainedTypedArray

Code:

public View getView(int i, View view, ViewGroup viewgroup)
{
    ImageView imageview;
    if (view == null)
    {
        imageview = new ImageView(b);
        imageview.setLayoutParams(new android.widget.AbsListView.LayoutParams(110, 110));
        imageview.setPadding(1, 1, 1, 1);
        imageview.setAdjustViewBounds(false);
        imageview.setScaleType(android.widget.ImageView.ScaleType.CENTER_CROP);
    } else
    {
        imageview = (ImageView)view;
    }
    imageview.setImageResource(a.getResources().obtainTypedArray(0x7f050000).getResourceId(i, -1)); //*warning*Typed Array should be recycled after use with #recycle()
    return imageview;
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Mojo Jojo
  • 173
  • 2
  • 11
  • So, I think you better make a typed array, set it with obtainTypedArray and then dismiss it properly with ta.recyle(); as soon as you don't need it anymore (that is, before the return statement). – Phantômaxx Jan 25 '14 at 18:31
  • buddy its showing an error `The method recyle() is undefined for the type MainActivity` – Mojo Jojo Jan 25 '14 at 18:34
  • Yes. In facts you should recycle your typed array, not the activity. In my comment I said **ta.recycle();**, not **this.recycle();**. It wasn't a typo. "ta" standing for "typed array", the one you should have created before using imageview.setImageResource.... – Phantômaxx Jan 25 '14 at 18:40
  • so you telling me to put `imageview.recycle();` before the end of return statement ? – Mojo Jojo Jan 25 '14 at 18:45
  • no, no, no. **ta.recycle();** Imagine you set **TypedArray ta = ...** then you use it, then you recycle it. – Phantômaxx Jan 25 '14 at 18:48
  • this means i have to put the Typed Array dot recycle that means `obtainTypedArray.recycle();` ? – Mojo Jojo Jan 25 '14 at 18:52
  • Please, have a look at the refence [site](http://developer.android.com/reference/android/content/res/TypedArray.html) – Phantômaxx Jan 25 '14 at 18:58

2 Answers2

18

You should hold onto the TypedArray you get back from obtainTypedArray() and call recycle() on it after using it.

Also, hard-coding a hex value like 0x7f050000 is unlikely to be the right answer.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @MojoJojo You don't (shouldn't) hard-code it. Use the name instead, such as `R.id.foo`. – dsh Nov 05 '15 at 18:35
14

use a recycle(); at the end of your obtainTypedArray() statement. do not use hex values, may lead to complications in your code in future.