在加载新活动时在Android应用程序中获取OutOfMemory异常

时间:2013-01-24 16:03:47

标签: android

12-11 23:38:42.568: ERROR/AndroidRuntime(4261): FATAL EXCEPTION: main
12-11 23:38:42.568: ERROR/AndroidRuntime(4261): android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at xxx.PickLeafCell.<init>(PickLeafCell.java:38)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at xxx.CueFileListAdapter.getView(CueFileListAdapter.java:108)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.ListView.fillSpecific(ListView.java:1272)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.ListView.layoutChildren(ListView.java:1558)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.View.layout(View.java:7035)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.View.layout(View.java:7035)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.View.layout(View.java:7035)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.View.layout(View.java:7035)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.View.layout(View.java:7035)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.os.Looper.loop(Looper.java:123)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at java.lang.reflect.Method.invoke(Method.java:521)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at dalvik.system.NativeStart.main(Native Method)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261): Caused by: java.lang.reflect.InvocationTargetException
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.ImageView.<init>(ImageView.java:108)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at java.lang.reflect.Constructor.constructNative(Native Method)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     ... 34 more
12-11 23:38:42.568: ERROR/AndroidRuntime(4261): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.graphics.Bitmap.nativeCreate(Native Method)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     at android.widget.ImageView.<init>(ImageView.java:118)
12-11 23:38:42.568: ERROR/AndroidRuntime(4261):     ... 38 more

2 个答案:

答案 0 :(得分:1)

您正在加载太大的位图。 Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget。但是有一个解决方案。您需要将位图解码为较低分辨率 [how to do so]

答案 1 :(得分:1)

你的问题在这里:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

似乎你正在加载一个大图像。为此,您有多种选择: 1.使用BitmapOptions获取大小,计算比例,然后仅使用您需要的比例进行解码

  1. 直接在PC上调整图像大小以满足设备需求。
  2. 此主题必读页面如下: Android developers

    PS。这里重要的是你永远不应该在UI线程上解码图像。这会影响您的应用效果。