加载视图寻呼机时出现奇怪的OutOfMemoryError

时间:2017-02-08 04:33:23

标签: android

我使用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)

以下是活动和视图寻呼机的源代码:

  1. 初始化视图寻呼机:
  2. 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. 适配器:
    2. 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. 带有空布局的片段
      2. 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 个答案:

答案 0 :(得分:1)

inflate调用是将新的MonthFragment添加到传入的容器中。

fragment.add()也在做同样的事情。这会创建一个不正确的图形,导致内存耗尽。

使用inflater.inflate(R.layout.fragment_month, container, false);对xml进行充气,但不会将其附加到视图中。这意味着fragments.add()只创建了一个片段副本,一切正常。