By Nguyen Minh Binh


2017-02-08 04:33:22 8 Comments

I created an activity that contain a view pager using FragmentStatePagerAdapter. The app crash immediately when launch this activity with an OutOfMemmoryError below. Please help to point me which mistake. I spend several hours but can't find myself.

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)

Here is the source code of the activity and view pager:

  1. Initialize the view pager:
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()));
  1. 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();
    }
}
  1. 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>

1 comments

@mksteve 2018-06-12 08:31:24

The inflate call is adding the new MonthFragment to the container passed in.

Also the fragments.add() is doing the same thing. This is creating an incorrect graph, which is causing the memory to run out.

Using inflater.inflate(R.layout.fragment_month, container, false); Inflates the xml, but does not attach it to the view. Meaning the fragments.add() only creates one copy of the fragment, and everything works.

Related Questions

Sponsored Content

25 Answered Questions

38 Answered Questions

[SOLVED] How to lazy load images in ListView in Android

43 Answered Questions

2 Answered Questions

1 Answered Questions

[SOLVED] Showing pre-selected item in viewpager

1 Answered Questions

Sponsored Content