Android - 视图创建时的内存不足异常

时间:2015-10-11 12:49:54

标签: android android-fragments

我尝试使用MaterialViewPager library。 每次运行我的应用程序时,我都会遇到以下异常。没有引用我自己的代码。

  

抛出OutOfMemoryError“无法分配6586008字节   分配4194304空闲字节和4MB直到OOM“

     

报告崩溃时出错

     

at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:125)   ....

调试后我发现我的代码停在以下行(OverviewPagerFragment - 完整代码见下文)

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d("Pager", "onCreateView");
    View view = inflater.inflate(R.layout.fragment_overview_pager, container, true);
    ButterKnife.bind(this, view);
    return view;
}

对于您的帮助,这是我的“OverviewActivity”的完整代码。第一个活动只是用“OverviewFragment”替换FrameLayout。它与库的示例代码完全相同,但我不知道抛出异常......

public class OverviewFragment extends BaseFragment<OverviewView, OverviewPresenter> {

    @Bind(R.id.material_view_pager)
    MaterialViewPager materialViewPager;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_overview;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("OverviewFragment", "onActivityCreated");
        materialViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Log.d("OverviewFragment", "getItem");
                return new OverviewPagerFragment();
            }

            @Override
            public int getCount() {
                Log.d("OverviewFragment", "getCount");
                return 4;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                Log.d("OverviewFragment", "getPageTitle");
                switch (position % 4) {
                    case 0:
                        return "Eins";
                    case 1:
                        return "Zwei";
                    case 2:
                        return "Drei";
                    case 3:
                        return "Vier";
                }

                return "";
            }
        });

        materialViewPager.getViewPager()
                .setOffscreenPageLimit(materialViewPager.getViewPager().getAdapter().getCount());
        materialViewPager.getPagerTitleStrip().setViewPager(materialViewPager.getViewPager());
    }
}



public class OverviewPagerFragment extends Fragment {

    RecyclerView.Adapter adapter;

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("Pager", "onCreateView");
        View view = inflater.inflate(R.layout.fragment_overview_pager, container, true);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d("Pager", "onViewCreated");
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);

        adapter = new RecyclerViewMaterialAdapter(new OverviewAdapter());
        recyclerView.setAdapter(adapter);
        MaterialViewPagerHelper.registerRecyclerView(getActivity(), recyclerView, null);
    }
}


public class OverviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    @Override
    public int getItemCount() {
        Log.d("Adapter", "getItemCount");
        return 4;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d("Adapter", "obCreateViewH");
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.overview_item_list, parent, false);

        return new RecyclerView.ViewHolder(view){};
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }
}

任何人都可以帮助我吗?我试着找几个小时的错误..谢谢

1 个答案:

答案 0 :(得分:0)

我有类似的问题。

更改行:

View view = inflater.inflate(R.layout.fragment_overview_pager, container, true); 

为:

View view = inflater.inflate(R.layout.fragment_overview_pager, container, false);