0

这是在我的 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)

任何机构都可以帮助解决这个问题。谢谢你

4

1 回答 1

1

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

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

  2. 考虑到应用程序的任何其他内存要求,您愿意承诺加载此图像的内存量。

  3. 要加载图像的目标 ImageView 或 UI 组件的尺寸。

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

例如,如果一个 1024x768 像素的图像最终会在 ImageView 中显示为 128x96 像素的缩略图,那么就不值得将其加载到内存中。

有关更多详细信息,请阅读

于 2016-07-03T12:37:02.587 回答