gpt4 book ai didi

ArrayList 和 RecyclerView 的 java.lang.OutOfMemoryError

转载 作者:行者123 更新时间:2023-12-01 11:04:45 26 4
gpt4 key购买 nike

提前感谢您的帮助。我试图将 ArrayList 放入 RecyclerView,但收到此错误:

java.lang.OutOfMemoryError: Failed to allocate a 7224906 byte allocation with 4194304 free bytes and 4MB until OOM at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95) at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125) at java.lang.StringBuffer.append(StringBuffer.java:278) at java.io.StringWriter.write(StringWriter.java:123) at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358) at com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303) at com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625) at com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658) at java.io.PrintWriter.append(PrintWriter.java:691) at java.io.PrintWriter.append(PrintWriter.java:31) at java.lang.Throwable.printStackTrace(Throwable.java:324) at java.lang.Throwable.printStackTrace(Throwable.java:300) at android.util.Log.getStackTraceString(Log.java:335) at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:59) at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:43) at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:85) at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

我的代码(有问题的部分)是这样的:

public class TabFragment5 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv = (RecyclerView) inflater.inflate(
R.layout.fragment_pat_list, container, false);
setupRecyclerView(rv);
return rv;
}

private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(),
getList(Infos.sPatGrandi)));
}


private ArrayList<String> getList(String[] array) {
ArrayList<String>list = new ArrayList<String>getList(Infos.sPatGrandi));
while (list.size()<40){
list.addAll(getList(Infos.sPatGrandi));
}
return list;
}

我只在这部分代码中遇到此错误,有人可以解释一下原因吗?非常感谢,祝您玩得愉快!

最佳答案

您在方法 getList 中进行了递归调用,由于该递归调用而分配了如此多的对象。这会产生您的 OutOfMemoryException

编辑示例:

List<String> listOfData = new ArrayList<>();
SimpleStringRecyclerViewAdapter adapter = new SimpleStringRecyclerView(context, listOfData);
recyclerView.setAdapter(adapter);
//Now add more content to the list
listOfData.add("new item");
listOfData.add("another item");
//This notifies the adapter and the list that the content has changed
adapter.notifyDataSetChanged();

关于ArrayList 和 RecyclerView 的 java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33066911/

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