Java 使用片段时出现内存不足错误

Java 使用片段时出现内存不足错误,java,android,android-fragments,tabs,fragment,Java,Android,Android Fragments,Tabs,Fragment,我第一次使用Fragment我在活动中放了3个片段, 问题是,当我第一次(仅第一次)打开“活动”时,会出现以下错误: 04-28 17:51:02.539 5001-5001/com.example.dell.jsonapplication E/dalvikvm-heap: Out of memory on a 22125220-byte allocation. 04-28 17:51:04.659 2427-2427/? E/Thermal-daemon: [flash_led

我第一次使用Fragment我在活动中放了3个片段, 问题是,当我第一次(仅第一次)打开“活动”时,会出现以下错误:

04-28 17:51:02.539 5001-5001/com.example.dell.jsonapplication E/dalvikvm-heap: Out of memory on a 22125220-byte allocation.
        04-28 17:51:04.659 2427-2427/? E/Thermal-daemon: [flash_led] temp_new :30  temp_old :31
虽然我的应用程序很小! 下面是包含片段的活动:

package com.example.dell.jsonapplication;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class GuidActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guid);


    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_guid, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new Guid1();
            case 1:
                return new Guid2();
            case 2:
                return new Guid3();
            default :
                return null;

        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}
}
下面是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".GuidActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

</android.support.v4.view.ViewPager>

<Button
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/back_frag"
    app:layout_anchor="@+id/container"
    app:layout_anchorGravity="left|center" />

<Button
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/next_frag"
    app:layout_anchor="@+id/container"
    app:layout_anchorGravity="right|center" />

<ImageView
    android:layout_width="140dp"
    android:layout_height="30dp"
    android:layout_gravity="center"
    android:background="@drawable/frag_progress"
    app:layout_anchor="@+id/container"
    app:layout_anchorGravity="bottom|center" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_menu_share" />

请帮助:/

而不是:

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new Guid1();
            case 1:
                return new Guid2();
            case 2:
                return new Guid3();
            default :
                return null;

        }
    }
你为什么不:

List<Fragment> myFragments = new ArrayList();

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
    myFragments.add(new Guid1());
    myFragments.add(new Guid2());
    myFragments.add(new Guid3());
}

@Override
public Fragment getItem(int position) {
    if (position < myFragments.size()) {
        return myFragments.get(position);
    } else {
        return null;
    }
}
下面是我用来创建自己的适配器的公式:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /**Adds a fragment and corresponding title to the list*/
    public void addFragment(Fragment f, String title){
        mFragmentList.add(f);
        mFragmentTitleList.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}
公共类节SpagerAdapter扩展了FragmentStatePagerAdapter{
私有最终列表MFFragmentList=new ArrayList();
私有最终列表MFFragmentTitleList=new ArrayList();
公共部分SpagerAdapter(碎片管理器fm){
超级(fm);
}
/**将片段和相应的标题添加到列表中*/
public void addFragment(片段f,字符串标题){
添加(f);
MFFragmentTitleList.add(标题);
}
@凌驾
公共片段getItem(int位置){
返回MFFragmentList.get(位置);
}
@凌驾
public int getCount(){
返回MFFragmentList.size();
}
}

我尝试了许多不同的代码,最后我尝试了以下方法:

android:largeHeap="true"

在Manifasets中,它对我很有用。

同样的错误:(我在GuidActivity类的开头发现了这个注释:/***将为每个部分提供*片段的{@link android.support.v4.view.PagerAdapter}。我们使用*{@link FragmentPagerAdapter}派生,它会将每个*加载的片段都保留在内存中。如果内存太大,最好切换到*{@link android.support.v4.app.FragmentStatePagerAdapter}.*/Yep.绝对正确。如果确实有效,请发布您的解决方案,并将其标记为正确,以供将来的查看者使用;),事实上,我不知道怎么做,你能解释一下我的代码应该做什么修改吗(我是新的片段:o)#Luís Henriques需要你的帮助。是的。很高兴听到这个消息。但它当然有效。您正在为应用程序分配大量内存。问题是,对于这样一个简单的应用程序,您不应该需要很大的堆。你的应用程序仍有问题。但如果你同意的话,没关系。如果你认为它合适,就把它标为正确答案。
android:largeHeap="true"