JSON Out of Memory error
<p>The application I made worked fine on my phone. When I launched it on the Google Play Store as a alpha version, i've had some reports/crashes of an Out of Memory error.</p>

<p>The error that is send:</p>

<pre><code>android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class &lt;unknown&gt;
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:411)
at android.app.Activity.setContentView(Activity.java:2177)
at com.leondrumsapp.leondrums.leondrumsapp.activity.LesSelectionActivity.setButtonLessons(LesSelectionActivity.java:157)
at com.leondrumsapp.leondrums.leondrumsapp.activity.LesSelectionActivity$1.onSuccess(LesSelectionActivity.java:139)
at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:152)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class &lt;unknown&gt;
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
... 14 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
... 19 more
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 74649612 byte allocation with 16777216 free bytes and 67MB 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:2650)
at android.content.res.Resources.loadDrawable(Resources.java:2555)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.view.View.&lt;init&gt;(View.java:3961)
at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:573)
at android.widget.LinearLayout.&lt;init&gt;(LinearLayout.java:203)
at android.widget.LinearLayout.&lt;init&gt;(LinearLayout.java:199)
at android.widget.LinearLayout.&lt;init&gt;(LinearLayout.java:195)
</code></pre>

<p>The code where is goes wrong:</p>

<pre><code>public void getLessons(){

    APIClient.get(url, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject object) {

            if (object != null) {
                try {
                    JSONArray jsonLesson = object.getJSONArray("drumlessons");
                    mLessons = new ArrayList&lt;&gt;();
                    for (int Index = 0; Index &lt; jsonLesson.length(); Index++) {

                        try {
                            JSONObject jsonLes = jsonLesson.getJSONObject(Index);
                            Lesson lesson = new Lesson();

                            lesson.id = jsonLes.getInt("_id");
                            lesson.number = jsonLes.getInt("lesnumber");
                            lesson.videoId = jsonLes.getString("videourl");

                            mLessons.add(lesson);

                        } catch (JSONException e) {
                            e.printStackTrace();
                            return;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    return;
                }

            }
            setButtonLessons();
        }


 public static ArrayList&lt;Lesson&gt; getArrayLesson() {
    return mLessons;
}

public void setButtonLessons() {

    //draw LesSelection
    setContentView(R.layout.activity_drumles);

    TableLayout layout = (TableLayout) findViewById(R.id.les_select_layout);

    int buttonIdCounter = 0;

    int columnCounter = 0;

    TableRow tr = new TableRow(this);
    TableRow.LayoutParams params = new TableRow.LayoutParams(
            TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
    params.setMargins(30, 0, 30, 0);
    tr.setLayoutParams(params);
    layout.addView(tr);

    for (int i=0; i&lt;1; i++) {

    for (Lesson lesson : getArrayLesson()) {

        if (columnCounter % LESSON_ROW_COUNT == 0) {
            tr = new TableRow(this);
            params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            params.setMargins(30, 0, 30, 0);
            tr.setLayoutParams(params);
            layout.addView(tr);
        }


            Button button = new Button(this);

            button.setId(buttonIdCounter);
            button.setText(String.valueOf(lesson.getId()));
            button.setOnClickListener(this);
            button.setBackgroundResource(R.drawable.buttonsoranje);
            TableRow.LayoutParams paramsRow = new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);

            paramsRow.column = columnCounter % LESSON_ROW_COUNT;
            params.gravity = Gravity.CENTER_HORIZONTAL;

            tr.addView(button);

            buttonIdCounter++;
            columnCounter = (columnCounter + 1) % LESSON_ROW_COUNT;
        }
    }

}
</code></pre>

<p>XML layout:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/achtergrond2"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:paddingRight="20dp"&gt;

&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="65dp"&gt;

    &lt;Button
        android:id="@+id/levelselection_back_button"
        android:paddingLeft="10pt"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/appicon"
        android:onClick="backButtonPressed"
        /&gt;
    &lt;TextView
        android:id="@+id/levelselection_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:textSize="26sp"
        android:text="Drumles overzicht"
        android:textColor="#000000"
        android:layout_alignBottom="@+id/levelselection_back_button"
        android:layout_toRightOf="@+id/levelselection_back_button"
        android:onClick="backButtonPressed"/&gt;
&lt;/RelativeLayout&gt;

&lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"&gt;
    &lt;TableLayout
        android:id="@+id/les_select_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:shrinkColumns="*"
        android:stretchColumns="field"
        android:layout_gravity="center_horizontal"&gt;

     &lt;/TableLayout&gt;
  &lt;/ScrollView&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>When I debug I noticed that the setButtonLessons has the full 101 json list, this probably causes the OOM crash. Is there a way that I can load this arraylist in blocks of mayby 10 instead of the entire json array (which is 101 atm but will get bigger).</p>

<p>After removing the button.setBackgroundResource(R.drawable.buttonsoranje); by adding "//" infront of it, i updated the app and asked a friend to test is. He came with the following error:</p>

<pre><code>android.view.InflateException: Binary XML file line #16: Error inflating class &lt;unknown&gt;
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:450)
at android.app.Activity.setContentView(Activity.java:2358)
at com.leondrumsapp.leondrums.leondrumsapp.activity.LesSelectionActivity.setButtonLessons(LesSelectionActivity.java:151)
at com.leondrumsapp.leondrums.leondrumsapp.activity.LesSelectionActivity$1.onSuccess(LesSelectionActivity.java:133)
at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:152)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6873)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
... 21 more
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 2359308 byte allocation with 1112964 free bytes and 1086KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:837)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:656)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1037)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3979)
at android.content.res.Resources.loadDrawable(Resources.java:3852)
at android.content.res.TypedArray.getDrawable(TypedArray.java:776)
at android.view.View.&lt;init&gt;(View.java:3997)
at android.widget.TextView.&lt;init&gt;(TextView.java:1028)
at android.widget.Button.&lt;init&gt;(Button.java:115)
at android.widget.Button.&lt;init&gt;(Button.java:108)
at android.widget.Button.&lt;init&gt;(Button.java:104)
... 24 more
</code></pre>