使用Fragment时内存不足ERROR

时间:2018-04-30 13:32:24

标签: java android android-fragments tabs fragment

我第一次使用片段我在活动中放了3个片段, 问题是当我第一次打开活动时(仅第一次)我收到此错误:

fixedContent:=StrReplace(myContent, "“”", "foo")

虽然我的应用程序非常小! 这是包含片段的活动:

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

这是xml文件:

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;
    }
}
}

请帮忙:/

2 个答案:

答案 0 :(得分:0)

而不是:

    @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;
    }
}

在研究了你的代码之后,我的猜测是你用堆片实例填充堆。使用此方法,您可以保证在应用程序启动时仅在创建适配器时创建片段。我们只是在应用程序运行时将它们存储在内存中,而不是每次要呈现片段时都创建一个新的。

如果这有帮助,请告诉我。

更新:

我通常在addFragment()的帮助下创建我的片段oustide the Adapter。

像这样:

// here we set up our own adapter...
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// ...add the fragments we want...
mSectionsPagerAdapter.addFragment(new Guid1());
mSectionsPagerAdapter.addFragment(new Guid2());
mSectionsPagerAdapter.addFragment(new Guid3());

// ...and set up our own adapter on the viewpager
mViewPager.setAdapter(mSectionsPagerAdapter);

这是我用来创建自己的适配器的公式:

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();
    }
}

答案 1 :(得分:0)

我尝试了很多不同的代码,最后我尝试了这个:

android:largeHeap="true"

在Manifasets中,它对我有用。