JSON Out of Memory错误

时间:2016-04-29 11:19:11

标签: java android json arraylist

我制作的应用程序在我的手机上运行良好。当我在Google Play商店中将其作为alpha版本推出时,我遇到了一些内存不足错误的报告/崩溃。

发送的错误:

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)

代码出错:

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布局:

<?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>

当我调试时,我注意到setButtonLessons有完整的101 json列表,这可能导致OOM崩溃。有没有办法可以在mayby 10的块中加载这个arraylist而不是整个json数组(101 atm但会变大)。

删除button.setBackgroundResource(R.drawable.buttonsoranje);通过在其前面添加“//”,我更新了应用程序,并要求朋友进行测试。他带来了以下错误:

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

1 个答案:

答案 0 :(得分:1)

这种错误是由于drawble ressources所以问题是以下几行:

button.setBackgroundResource(R.drawable.buttonsoranje);