在Android设备上调试时出现内存不足错误

时间:2016-07-03 12:16:21

标签: android android-studio android-debugging

这是在我的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:817)
                  at android.content.res.Resources.loadDrawable(Resources.java:2854)
                  at android.content.res.Resources.getDrawable(Resources.java:1521)
                  at android.widget.ImageView.resolveUri(ImageView.java:628)
                  at android.widget.ImageView.setImageResource(ImageView.java:361)
                  at com.example.prof_mohamed.ksa.accmanagerAdapter.getView(accmanagerAdapter.java:72)
                  at android.widget.AbsListView.obtainView(AbsListView.java:2570)
                  at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
                  at android.widget.ListView.onMeasure(ListView.java:1165)
                  at android.view.View.measure(View.java:16420)
                  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChild(ViewGroup.java:5025)
                  at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:393)
                  at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:419)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2549)
                  at android.view.View.measure(View.java:16420)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2188)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1333)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1544)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5089)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                  at android.view.Choreographer.doFrame(Choreographer.java:561)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
                  at android.os.Handler.handleCallback(Handler.java:725)
                  at android.os.Handler.dispatchMessage(Handler.java:92)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.A*ctivityThread.main(ActivityThread.java:5283)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                  at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮助解决这个问题。谢谢

1 个答案:

答案 0 :(得分:1)

要避免 java.lang.OutOfMemory 异常,请在解码之前检查位图的尺寸,除非您完全信任该源为您提供可预测大小的图像数据,这些数据可以轻松地放入可用内存中。将缩小版本加载到内存中。由于图像尺寸已知,因此可以使用它们来确定是否应将完整图像加载到内存中,或者是否应加载子采样版本。以下是需要考虑的一些因素:

  1. 估计在内存中加载完整图像的内存使用情况。

  2. 根据应用程序的任何其他内存要求,您愿意承诺加载此映像的内存量。

  3. 图像所在的目标ImageView或UI组件的尺寸 被装入。

  4. 当前设备的屏幕尺寸和密度。

  5. 例如,如果最终将在ImageView中以128x96像素的缩略图显示,则不值得将1024x768像素图像加载到内存中。

    有关详情,请参阅this