繁体   English   中英

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

[英]Out of memory ERROR when using Fragment

我第一次使用Fragment时,在我的活动中放入了3个Fragments,问题是当我第一次(仅第一次)打开活动时遇到此错误:

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

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

让我知道是否有帮助。


更新:

我通常在addFragment()的帮助下创建Fragments来取代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();
    }
}

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

android:largeHeap="true"

在Manifasets中,它对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM