0

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.

The error that is send:

android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
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 <unknown>
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.<init>(View.java:3961)
at android.view.ViewGroup.<init>(ViewGroup.java:573)
at android.widget.LinearLayout.<init>(LinearLayout.java:203)
at android.widget.LinearLayout.<init>(LinearLayout.java:199)
at android.widget.LinearLayout.<init>(LinearLayout.java:195)

The code where is goes wrong:

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<>();
                    for (int Index = 0; Index < 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<Lesson> 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<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;
        }
    }

}

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<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">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="65dp">

    <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"
        />
    <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"/>
</RelativeLayout>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
    <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">

     </TableLayout>
  </ScrollView>
</LinearLayout>

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).

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:

android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
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.<init>(View.java:3997)
at android.widget.TextView.<init>(TextView.java:1028)
at android.widget.Button.<init>(Button.java:115)
at android.widget.Button.<init>(Button.java:108)
at android.widget.Button.<init>(Button.java:104)
... 24 more
Jaxhead
  • 69
  • 1
  • 8

1 Answers1

1

This kind of error is due to drawble ressources so the problem is the following line :

button.setBackgroundResource(R.drawable.buttonsoranje);
TooCool
  • 10,598
  • 15
  • 60
  • 85
  • I red on [another post](http://stackoverflow.com/questions/14096061/android-setbackgroundresource-cause-out-of-memory-excepiton) that the solution would be adding the icons in an other drawable folder, the drawable-nodpi. This would case them not to be able to resize, but they aren't resized anyhow. Can i just create this folder myself? Reason i ask is, i cannot test this OOM error since it's not acccuring on my phone. – Jaxhead Apr 29 '16 at 12:19
  • yeah you can create it – TooCool Apr 29 '16 at 12:23
  • i tried removing the button.setBackgroundResource(R.drawable.buttonsoranje); and updated the app. I posted a new error in the thread. Seems like the same problem – Jaxhead Apr 29 '16 at 13:15
  • Well anyhow: the images you try to use are to big. – greenapps Apr 29 '16 at 13:31
  • But when i remove them, they aren't using the drawable buttons, just regular buttons, and it still crashes? – Jaxhead Apr 29 '16 at 13:33
  • Really? With OutOfMemory on those bitmap actions? – greenapps Apr 29 '16 at 13:39
  • I posted the error in the above thread, it is the last section. – Jaxhead Apr 29 '16 at 13:52