问题描述
- Android中xml报oom,怎么解决
-
最近项目总是碰到oom,一个界面直接点进去不会报oom,但是如果其他界面点击多了,再点击那个界面就会报xml的oom,定位到xml那一行就是一个ImageView的src设置了一个背景图片。
感觉就是有内存没有回收,不然如果是图片太大,第一次应该会oom,但是第一次直接点不会报oom,其他界面点多了再点那个界面就会报oom错误截图
04-15 11:27:51.711: E/AndroidRuntime(13268): FATAL EXCEPTION: main 04-15 11:27:51.711: E/AndroidRuntime(13268): Process: com.example.text, PID: 13268 04-15 11:27:51.711: E/AndroidRuntime(13268): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.text/com.example.peoinfo.Peo_Info}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class <unknown> 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2421) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.ActivityThread.access$900(ActivityThread.java:153) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.os.Handler.dispatchMessage(Handler.java:102) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.os.Looper.loop(Looper.java:148) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.ActivityThread.main(ActivityThread.java:5432) 04-15 11:27:51.711: E/AndroidRuntime(13268): at java.lang.reflect.Method.invoke(Native Method) 04-15 11:27:51.711: E/AndroidRuntime(13268): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735) 04-15 11:27:51.711: E/AndroidRuntime(13268): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 04-15 11:27:51.711: E/AndroidRuntime(13268): Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class <unknown> 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.inflate(LayoutInflater.java:543) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 04-15 11:27:51.711: E/AndroidRuntime(13268): at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.Activity.setContentView(Activity.java:2183) 04-15 11:27:51.711: E/AndroidRuntime(13268): at com.example.peoinfo.Peo_Info.onCreate(Peo_Info.java:60) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.Activity.performCreate(Activity.java:6303) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374) 04-15 11:27:51.711: E/AndroidRuntime(13268): ... 9 more 04-15 11:27:51.711: E/AndroidRuntime(13268): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class <unknown> 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.createView(LayoutInflater.java:649) 04-15 11:27:51.711: E/AndroidRuntime(13268): at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:698) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.rInflate(LayoutInflater.java:842) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.inflate(LayoutInflater.java:519) 04-15 11:27:51.711: E/AndroidRuntime(13268): ... 17 more 04-15 11:27:51.711: E/AndroidRuntime(13268): Caused by: java.lang.reflect.InvocationTargetException 04-15 11:27:51.711: E/AndroidRuntime(13268): at java.lang.reflect.Constructor.newInstance(Native Method) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.view.LayoutInflater.createView(LayoutInflater.java:623) 04-15 11:27:51.711: E/AndroidRuntime(13268): ... 26 more 04-15 11:27:51.711: E/AndroidRuntime(13268): Caused by: java.lang.OutOfMemoryError: Failed to allocate a 21454860 byte allocation with 16672552 free bytes and 15MB until OOM 04-15 11:27:51.711: E/AndroidRuntime(13268): at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:446) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.content.res.Resources.createFromResourceStream(Resources.java:2948) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.content.res.Resources.loadDrawableForCookie(Resources.java:2680) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.content.res.Resources.loadDrawable(Resources.java:2580) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:387) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.content.res.TypedArray.getDrawable(TypedArray.java:872) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.widget.ImageView.<init>(ImageView.java:152) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.widget.ImageView.<init>(ImageView.java:140) 04-15 11:27:51.711: E/AndroidRuntime(13268): at android.widget.ImageView.<init>(ImageView.java:136) 04-15 11:27:51.711: E/AndroidRuntime(13268): ... 28 more
解决方案
Android 解决BitmapFactory.decodeFile(file) 报OOM问题
Android处理图片报OOM的问题解决
Android加载Bitmap之OOM的解决办法
解决方案二:
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class 12行
解决方案三:
使用Picasso加载图片可以避免OOM
时间: 2024-12-24 21:16:02