并提前感谢您的帮助。 我试图将ArrayList放入RecyclerView,但我收回了这个错误:
java.lang.OutOfMemoryError:无法分配7224906字节分配4194304空闲字节和4MB直到OOM at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95) at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125) 在java.lang.StringBuffer.append(StringBuffer.java:278) at java.io.StringWriter.write(StringWriter.java:123) 在com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358) 在com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303) 在com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625) 在com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658) 在java.io.PrintWriter.append(PrintWriter.java:691) 在java.io.PrintWriter.append(PrintWriter.java:31) 在java.lang.Throwable.printStackTrace(Throwable.java:324) 在java.lang.Throwable.printStackTrace(Throwable.java:300) 在android.util.Log.getStackTraceString(Log.java:335) 在com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:59) 在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) 在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;
}
我只用这部分代码得到这个错误,有人可以解释一下原因吗? 非常感谢,祝你玩得愉快!
答案 0 :(得分:1)
您在方法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();