gpt4 book ai didi

java - JSON 内存不足错误

转载 作者:行者123 更新时间:2023-12-01 09:59:24 25 4
gpt4 key购买 nike

我制作的应用程序在我的手机上运行良好。当我在 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 崩溃。有没有一种方法可以让我以 10 block 为单位加载这个数组列表,而不是整个 json 数组(这是 101 atm,但会变得更大)。

删除button.setBackgroundResource(R.drawable.buttonsoranje);后通过在其前面添加“//”,我更新了应用程序并请 friend 测试。他遇到了以下错误:

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

最佳答案

这种错误是由于可绘制资源引起的,因此问题是以下行:

button.setBackgroundResource(R.drawable.buttonsoranje);

关于java - JSON 内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36936999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com