我使用FragmentStatePagerAdapter
创建了一个包含视图寻呼机的活动。使用下面的OutOfMemmoryError
启动此活动时,应用程序立即崩溃。请帮我指出哪个错误。我花了几个小时,但找不到自己。
02-07 23:04:58.187 16037-16037/com.makeit.lite E/AndroidRuntime: Error reporting crash
java.lang.OutOfMemoryError: Failed to allocate a 34831542 byte allocation with 16765168 free bytes and 31MB 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:687)
at java.io.Writer.append(Writer.java:198)
at java.lang.Throwable.printStackTrace(Throwable.java:324)
at java.lang.Throwable.printStackTrace(Throwable.java:300)
at android.util.Log.getStackTraceString(Log.java:343)
at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:61)
at com.android.internal.os.RuntimeInit.-wrap0(RuntimeInit.java)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:86)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
这是活动和查看寻呼机的源代码:
初始化视图分页器:
fragments.add(new MonthFragment()); fragments.add(new MonthFragment()); fragments.add(new MonthFragment()); fragments.add(new MonthFragment()); fragments.add(new MonthFragment()); fragments.add(new MonthFragment()); setContentView(R.layout.activity_date_picker); ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); viewPager.setAdapter(new MonthAdapter(getSupportFragmentManager()));
- The adapter:
class MonthAdapter extends FragmentStatePagerAdapter { public MonthAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } }
- Fragment with empty layout
public class MonthFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_month, container); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"/>
</LinearLayout>
最佳答案
inflate
调用将新的MonthFragment添加到传入的容器中。
另外fragments.add()也在做同样的事情。这将创建不正确的图形,从而导致内存用完。
使用inflater.inflate(R.layout.fragment_month, container, false);
使xml膨胀,但不将其附加到视图。意味着fragments.add()仅创建该片段的一个副本,并且一切正常。