Search code examples
androidout-of-memoryinflate-exceptioninvocationtargetexception

android.view.InflateException, java.lang.reflect.InvocationTargetException, java.lang.OutOfMemoryError


I am facing these three errors that crash my App.

Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.OutOfMemoryError

Some on this website suggested that the image file may be too large that causes the out of memory error which in turn causes the two other errors. The image I used is about 50KB, too small, I tried even a blank image of about 4KB but the problem is still there. Below is my code, could anyone please help me solve this problem.

public class occasions extends Activity {

TextView linkToPageTextView;
String LinkToPageTxt = "Click to visit Page";

ImageView headerImageView;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview_options);

    GridView gridView = (GridView)findViewById(R.id.gridview);
    gridView.setAdapter(new MyAdapter(this));
    gridView.setNumColumns(2);

    headerImageView = (ImageView) findViewById(R.id.header_img);
    headerImageView.setBackgroundResource(R.drawable.stock);

    linkToPageTextView = (TextView) findViewById(R.id.link_to_page_text);
    linkToPageTextView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // Do something
        }
    });
}

private class MyAdapter extends BaseAdapter
{
    private List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;

    ImageView option_picture;
    TextView option_name;

    public MyAdapter(Context context)
    {
        inflater = LayoutInflater.from(context);

        items.add(new Item("A", R.drawable.ps_tousled));
        items.add(new Item("B", R.drawable.ps_googiesdiner));
        items.add(new Item("C", R.drawable.ps_drugstore));
        items.add(new Item("D", R.drawable.ps_misc));       
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i)
    {
        return items.get(i);
    }

    @Override
    public long getItemId(int i)
    {
        return items.get(i).drawableId;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup)
    {
        View v = view;

        if(v == null)
        {
           v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
           v.setTag(R.id.picture, v.findViewById(R.id.picture));
           v.setTag(R.id.option_text, v.findViewById(R.id.option_text));
        }

        option_picture = (ImageView)v.getTag(R.id.picture);
        option_name = (TextView)v.getTag(R.id.option_text);

        Item item = (Item)getItem(i);

        option_picture.setImageResource(item.drawableId);
        option_name.setText(item.name);

        option_name.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent album_Activity = new Intent(occasions.this, 
                        GridViewActivity.class);

                if(i == 0)
                    {
                        album_Activity.putExtra("album_ID", "Series");
                }
                else if(i == 1)
                {
                    album_Activity.putExtra("album_ID", "Christmas");
                }
                else if(i == 2)
                {
                    album_Activity.putExtra("album_ID", "NY");

                }
                else if(i == 3)
                {

                    album_Activity.putExtra("album_ID", "Misc");
                }

                album_Activity.putExtra("sub_ID", 0);
                startActivity(album_Activity);
            }
        });

        return v;
    }
}

private class Item
{
    final String name;
    final int drawableId;

    Item(String name, int drawableId)
    {
        this.name = name;
        this.drawableId = drawableId;
    }
}
}

and the xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:padding="5dp">

<com.jbd.abc.SquareImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    />
<TextView
    android:id="@+id/option_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:layout_gravity="bottom"
    android:textColor="@android:color/white"
    android:background="#55000000"
    android:textStyle="bold"
    android:textSize="22dip"
    android:textIsSelectable="true"
    />
</FrameLayout>

Solution

  • public static Bitmap decodeFile(int filePath, int WIDTH, int HIGHT)
    {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        //BitmapFactory.decodeStream(new FileInputStream(f), null, o);
        BitmapFactory.decodeStream(_context.getResources().openRawResource(filePath),
                null,o);
    
        final int REQUIRED_WIDTH = WIDTH;
        final int REQUIRED_HIGHT = HIGHT;
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
            scale *= 2;
    
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        //return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        return BitmapFactory.decodeStream(_context.getResources().openRawResource(filePath),
                null, o2);
    }
    

    and adding the line to the manifest file.

    android:largeHeap="true"