获得致命的异常:主java.lang.OutOfMemoryError

时间:2016-12-10 00:24:16

标签: java android

我收到此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.OutOfMemoryError
                  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
                  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
                  at android.content.res.Resources.loadDrawable(Resources.java:2988)
                  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                  at android.view.View.<init>(View.java:3563)
                  at android.widget.ImageView.<init>(ImageView.java:125)
                  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                  at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1013)
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1072)
                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                  at mario.fragments.SplashScreenSalir_Fragment.onCreateView(SplashScreenSalir_Fragment.java:30)
                  at android.app.Fragment.performCreateView(Fragment.java:1699)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
                  at android.app.BackStackRecord.run(BackStackRecord.java:682)
                  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
                  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
                  at android.os.Handler.handleCallback(Handler.java:730)
                  at android.os.Handler.dispatchMessage(Handler.java:92)
                  at android.os.Looper.loop(Looper.java:176)
                  at android.app.ActivityThread.main(ActivityThread.java:5419)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:525)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                  at dalvik.system.NativeStart.main(Native Method)
E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.Restarter.getActivities
W/dalvikvm: VFY: unable to resolve instanceof 2541 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/Restarter;
D/dalvikvm: VFY: replacing opcode 0x20 at 0x006f

12/10 01:19:15: Launching app
I/Process: Sending signal. PID: 28067 SIG: 9
Application terminated.

我一直在阅读它,但我刚刚阅读了有关虚拟机的信息,我正在使用自己的手机获取它。

这有点奇怪,因为代码实际上有效。我的意思是,在崩溃后,如果我再次按下按钮,它可以工作(我附上几张图片)

enter image description here

enter image description here

我的片段代码只是一个简单的SplashScreen:

      package mario.fragments;


    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import mario.rankit.MainActivity;
    import mario.rankit.R;

        /**
         * A simple {@link Fragment} subclass.
         */
        public class SplashScreenSalir_Fragment extends Fragment {
            View view;

            pu

blic SplashScreenSalir_Fragment() {
            // Required empty public constructor
        }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_splash_screen_salir_, container, false);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(view.getContext(), MainActivity.class);
                startActivity(intent);
            }
        }, 3000);

        return view;
    }

}

你知道怎么解决吗?

1 个答案:

答案 0 :(得分:1)

你正在使用非常大的图像。您可能需要减小其大小。您不需要这样的高分辨率位图。 2048乘2048或1024乘1024位图占据如此巨大的内存。想象一下有多个这样的图像?

实际上有两种解决方案:

使用Glide,您可以在运行时缩小图像大小。

例如:

Glide  
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200)
    .fitCenter() 
    .into(imageViewResizeFitCenter);

详情请见: - Glide Image Resizing and Scaling