Fragment.onCreateView中的OutOfMemoryError

时间:2018-07-14 04:03:39

标签: android out-of-memory

我的Android应用程序在某些设备上运行良好,但在其他设备上崩溃。在我的开发人员控制台上,它在ChaptersFragment.onCreateView中显示OutOfMemoryError。我给了下面的代码。我是新手,这是我的第一个android应用程序。有解决方案吗?还是我做错了什么?

public class ChaptersFragment extends Fragment {
private CardView c1,c2,c3,c4,c5,c6,c7,c8,c9;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {
    View myView = inflater.inflate(R.layout.fragment_chapters, container, false);
    c1 = (CardView) myView.findViewById(R.id.chapter1);
    c2 = (CardView) myView.findViewById(R.id.chapter2);
    c3 = (CardView) myView.findViewById(R.id.chapter3);
    c4 = (CardView) myView.findViewById(R.id.chapter4);
    c5 = (CardView) myView.findViewById(R.id.chapter5);
    c6 = (CardView) myView.findViewById(R.id.chapter6);
    c7 = (CardView) myView.findViewById(R.id.chapter7);
    c8 = (CardView) myView.findViewById(R.id.chapter8);
    c9 = (CardView) myView.findViewById(R.id.chapter9);


    c1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap1.class);
            in.putExtra("some", "Introduction to Stock Markets");
            startActivity(in);
        }
    });

    c2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap2.class);
            in.putExtra("some", "Technical Analysis");
            startActivity(in);
        }
    });

    c3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap3.class);
            in.putExtra("some", "Fundamental Analysis");
            startActivity(in);
        }
    });

    c4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap4.class);
            in.putExtra("some", "Futures Trading");
            startActivity(in);
        }
    });

    c5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap5.class);
            in.putExtra("some", "Options Theory for Professional Trading");
            startActivity(in);
        }
    });

    c6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap6.class);
            in.putExtra("some", "Options Strategies");
            startActivity(in);
        }
    });

    c7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap7.class);
            in.putExtra("some", "Markets and Taxation");
            startActivity(in);
        }
    });

    c8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap8.class);
            in.putExtra("some", "Currency and Commodity Futures");
            startActivity(in);
        }
    });

    c9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), Chap9.class);
            in.putExtra("some", "Risk Management & Trading Psychology");
            startActivity(in);
        }
    });

    return myView;
 }
}

1 个答案:

答案 0 :(得分:0)

当您在布局上设置图像时,渲染器将尝试将其缩放到所需的大小并尝试将其容纳在屏幕上,在此过程中它将消耗一些内存。根据设备上可用的内存,它将轻松达到每个Android OS计算的应用程序的最大可用内存(百分比)。

700像素可能看起来并不多,但是如果您在布局中加载了9张图像,并且系统必须将某些图像调整为较低分辨率以适合您的屏幕,则用于处理该图像的必要内存之和将导致-内存错误。

这就是为什么您必须为应用程序中的每个目标设备分辨率创建包含不同图像大小的多个可绘制文件夹的原因。不必很精确,但是必须手动将图像调整为接近屏幕上所需的大小,以避免在渲染过程中占用内存。

http://www.androiddocs.com/guide/practices/screens_support.html