我创建了一个包含视图页面的活动,使用FragmentStatePagerAdapter
。当启动此活动时,应用程序立即崩溃,并出现以下OutOfMemoryError
错误。请帮忙指出我的错误。我花了几个小时,但找不到原因。
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()));
- 适配器:
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(); } }
- 空布局的片段
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>