本文介绍了获取此异常会强制 android 应用程序在启动时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

11-23 10:38:04.430 2095-2095/?I/art: 不延迟启用 -Xcheck:jni (已经启用)11-23 10:38:04.430 2095-2095/?I/art:后期启用 JIT11-23 10:38:04.433 2095-2095/?I/art:使用 code_cache_capacity=2MB compile_threshold=1000 创建的 JIT11-23 10:38:04.830 2095-2095/com.example.somanshusrivastava.carpiko W/系统:类加载器引用未知路径:/data/app/com.example.somanshusrivastava.carpiko-2/lib/x8611-23 10:38:05.697 2095-2106/com.example.somanshusrivastava.carpiko I/art:后台部分并发标记扫描 GC 释放 96(3KB) AllocSpace 对象,0(0B) LOS 对象,7% 空闲,50MB/54MB,暂停 2.581ms 共 140.616ms11-23 10:38:05.812 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.812 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.827 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc 粘性并发标记扫描 GC 释放 79(11KB) AllocSpace 对象,0(0B) LOS 对象,7% 空闲,50MB/54MB,暂停 5.901ms 总共 13.614ms11-23 10:38:05.836 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.850 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc 部分并发标记扫描 GC 释放 23(1008B) AllocSpace 对象,0(0B) LOS 对象,7% 免费,50MB/54MB,暂停 2.153ms 共 10.247ms11-23 10:38:05.853 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.867 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc 并发标记扫描 GC 释放 15(12KB) AllocSpace 对象, 0(0B) LOS 对象, 7% free, 50MB/54MB, 暂停 1.431ms 共 13.789ms11-23 10:38:05.868 2095-2095/com.example.somanshusrivastava.carpiko I/art: 强制收集软引用以分配 49MB11-23 10:38:05.869 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.881 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc 并发标记扫描 GC 释放 11(344B) AllocSpace 对象,0(0B) LOS 对象,7% 免费,50MB/54MB, 暂停 1.407ms 共 11.758ms11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko W/art: 抛出 OutOfMemoryError 无法分配 51750048 字节分配,4194304 可用字节和 13MB 直到 OOM"11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.892 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc 粘性并发标记扫描 GC 释放 3(608B) AllocSpace 对象,0(0B) LOS 对象,7% 免费,50MB/54MB,暂停 1.389ms 共 8.964ms11-23 10:38:05.894 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.903 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc 部分并发标记扫描 GC 释放 6(192B) AllocSpace 对象,0(0B) LOS 对象,7% 免费,50MB/54MB,暂停 1.387ms 共 8.867ms11-23 10:38:05.904 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.916 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc 并发标记扫描 GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, 暂停 1.416ms 共 10.922ms11-23 10:38:05.917 2095-2095/com.example.somanshusrivastava.carpiko I/art: 强制收集软引用以分配 49MB11-23 10:38:05.917 2095-2095/com.example.somanshusrivastava.carpiko I/art: 启动阻塞 GC Alloc11-23 10:38:05.928 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc 并发标记扫描 GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, 暂停 1.350ms 共 10.018ms11-23 10:38:05.929 2095-2095/com.example.somanshusrivastava.carpiko W/art: 抛出 OutOfMemoryError 无法分配 51750048 字节分配,4194304 可用字节和 13MB 直到 OOM"11-23 10:38:05.929 2095-2095/com.example.somanshusrivastava.carpiko D/skia: --- 缩放位图分配失败11-23 10:38:05.930 2095-2095/com.example.somanshusrivastava.carpiko D/AndroidRuntime: 关闭虚拟机11-23 10:38:05.930 2095-2095/com.example.somanshusrivastava.carpiko E/AndroidRuntime: 致命异常: main进程:com.example.somanshusrivastava.carpiko,PID:2095java.lang.OutOfMemoryError:无法分配 51750048 字节分配,其中 4194304 可用字节和 13MB 直到 OOM在 dalvik.system.VMRuntime.newNonMovableArray(本机方法)在 android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)在 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)在 android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)在 android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)在 android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)在 android.content.res.Resources.loadDrawable(Resources.java:2540)在 android.content.res.TypedArray.getDrawable(TypedArray.java:870)在 android.widget.ImageView.(ImageView.java:152)在 android.widget.ImageView.(ImageView.java:140)在 android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:58)在 android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:54)在 android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:95)在 android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)在 android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)在 android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)在 android.view.LayoutInflater.rInflate(LayoutInflater.java:835)在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)在 android.view.LayoutInflater.rInflate(LayoutInflater.java:838)在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)在 android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)在 android.view.LayoutInflater.rInflate(LayoutInflater.java:831)在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)在 android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)在 android.view.LayoutInflater.rInflate(LayoutInflater.java:831)在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)在 android.view.LayoutInflater.inflate(LayoutInflater.java:515)在 android.view.LayoutInflater.inflate(LayoutInflater.java:423)在 android.view.LayoutInflater.inflate(LayoutInflater.java:374)在 android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)在 android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)在 com.example.somanshusrivastava.carpiko.MainActivity.onCreate(MainActivity.java:24)在 android.app.Activity.performCreate(Activity.java:6237)在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)在 android.app.ActivityThread.-wrap11(ActivityThread.java)在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)在 android.os.Handler.dispatchMessage(Handler.java:102)在 android.os.Looper.loop(Looper.java:148)在 android.app.ActivityThread.main(ActivityThread.java:5417)在 java.lang.reflect.Method.invoke(本机方法)在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)11-23 10:38:07.560 2095-2102/com.example.somanshusrivastava.carpiko W/art:暂停所有线程耗时:13.233ms11-23 10:38:09.537 2095-2102/com.example.somanshusrivastava.carpiko W/艺术:暂停所有线程耗时:9.664ms11-23 10:38:11.140 2095-2095/?I/Process:发送信号.PID:2095 SIG:9

每当我尝试运行 AVD 时,我的应用程序都会意外关闭,这就是我得到的错误.这是生成的 logcat.它显示了一个 FatalException 和一个 MemoryException.是 AVD 错误还是代码错误?请帮忙.

解决方案

你需要了解Bitmap和内存的概念

假设您想在屏幕上显示您刚刚用相机拍摄的图像.为此所需的总内存使用以下公式计算:memory_needed_in_bytes = 4 * image_width * image_height;

为什么是 4?好吧,最常见/推荐的位图配置是 ARGB_8888.这意味着对于我们绘制的每个像素,我们需要在内存中为 alpha、红色、贪婪和蓝色通道保留 8 位(1 个字节),以便正确显示它.还有其他替代方案,例如 RGB_565 配置,它需要的内存是 ARGB_8888 的一半,但会丢失透明度和颜色精度(同时可能会添加绿色色调).

假设您拥有一台配备全高清屏幕和 12 MP 摄像头的全新设备.您刚刚拍摄的图片大小为 4000x3000 像素,显示它所需的总内存为:4 字节 * 4000 * 3000 = 48 MB

48 MB 的 RAM 仅用于单个图像!?太多了!

现在让我们考虑一下屏幕分辨率.您正在尝试在具有 1920x1080 像素的屏幕上显示 4000x3000 图像,在最坏的情况下(全屏显示图像)您不应分配超过 4 * 1920 * 1080 = 8.3 MB 的内存.

始终遵循有效显示位图的 Android 编程技巧:

测量您显示图像的视图.相应地缩放/裁剪大图像.只显示可以显示的内容.

11-23 10:38:04.430 2095-2095/? I/art: Not late-enabling -Xcheck:jni (already on)
11-23 10:38:04.430 2095-2095/? I/art: Late-enabling JIT
11-23 10:38:04.433 2095-2095/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
11-23 10:38:04.830 2095-2095/com.example.somanshusrivastava.carpiko W/System: ClassLoader referenced unknown path: /data/app/com.example.somanshusrivastava.carpiko-2/lib/x86
11-23 10:38:05.697 2095-2106/com.example.somanshusrivastava.carpiko I/art: Background partial concurrent mark sweep GC freed 96(3KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 2.581ms total 140.616ms
11-23 10:38:05.812 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.812 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.827 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc sticky concurrent mark sweep GC freed 79(11KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 5.901ms total 13.614ms
11-23 10:38:05.836 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.850 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc partial concurrent mark sweep GC freed 23(1008B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 2.153ms total 10.247ms
11-23 10:38:05.853 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.867 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 15(12KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.431ms total 13.789ms
11-23 10:38:05.868 2095-2095/com.example.somanshusrivastava.carpiko I/art: Forcing collection of SoftReferences for 49MB allocation
11-23 10:38:05.869 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.881 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.407ms total 11.758ms
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko W/art: Throwing OutOfMemoryError "Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM"
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.892 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc sticky concurrent mark sweep GC freed 3(608B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.389ms total 8.964ms
11-23 10:38:05.894 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.903 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.387ms total 8.867ms
11-23 10:38:05.904 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.916 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.416ms total 10.922ms
11-23 10:38:05.917 2095-2095/com.example.somanshusrivastava.carpiko I/art: Forcing collection of SoftReferences for 49MB allocation
11-23 10:38:05.917 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.928 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.350ms total 10.018ms
11-23 10:38:05.929 2095-2095/com.example.somanshusrivastava.carpiko W/art: Throwing OutOfMemoryError "Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM"
11-23 10:38:05.929 2095-2095/com.example.somanshusrivastava.carpiko D/skia: --- allocation failed for scaled bitmap
11-23 10:38:05.930 2095-2095/com.example.somanshusrivastava.carpiko D/AndroidRuntime: Shutting down VM
11-23 10:38:05.930 2095-2095/com.example.somanshusrivastava.carpiko E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.somanshusrivastava.carpiko, PID: 2095
                                                                                      java.lang.OutOfMemoryError: Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM
                                                                                          at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                                          at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                                          at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                                                                                          at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                                                                                          at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                                                                                          at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                                                                                          at android.content.res.Resources.loadDrawable(Resources.java:2540)
                                                                                          at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                                          at android.widget.ImageView.(ImageView.java:152)
                                                                                          at android.widget.ImageView.(ImageView.java:140)
                                                                                          at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:58)
                                                                                          at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:54)
                                                                                          at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:95)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
                                                                                          at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                          at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
                                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
                                                                                          at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                                          at com.example.somanshusrivastava.carpiko.MainActivity.onCreate(MainActivity.java:24)
                                                                                          at android.app.Activity.performCreate(Activity.java:6237)
                                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-23 10:38:07.560 2095-2102/com.example.somanshusrivastava.carpiko W/art: Suspending all threads took: 13.233ms
11-23 10:38:09.537 2095-2102/com.example.somanshusrivastava.carpiko W/art: Suspending all threads took: 9.664ms
11-23 10:38:11.140 2095-2095/? I/Process: Sending signal. PID: 2095 SIG: 9

Whenever I try to run the AVD, my app unexpectedly closes and this is the error I get. This is the logcat that is generated. It is showing a FatalException and a MemoryException. Is it an AVD error or code error? Please help.

解决方案

You need to understand the concept of Bitmap and memory

这篇关于获取此异常会强制 android 应用程序在启动时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 08:42