实质是Out Of Memory 的android.view.InflateException: Binary XML file line #75: Error inflating class

本文探讨了解决Android应用中遇到的内存溢出问题,通过案例分析发现真正原因在于ImageView占用大量内存,并通过替换背景图片大小成功解决。文中还整理了相关资料,帮助开发者深入理解内存管理。

今天解bug遇到android.view.InflateException问题


在索尼L36H上是正常的,而在天语手机上就报错了。


显示的错误行竟然是ImageView,而不是自定义的View


纠结了半天,才发现真正原因是out of memory错误。


网上搜索相关资料,有
http://stackoverflow.com/questions/7536988/android-app-out-of-memory-issues-tried-everything-and-still-at-a-loss/7576275
相关资料。


但是我并没有手动回收ImageView,只是将500K的背景图片换成100K的就好了。
Mark之。
### 解决Android中TextView类在加载XML布局时引发的InflateException问题 在Android开发中,`android.view.InflateException`通常表示在尝试从XML文件中加载视图时发生了错误。根据提供的信息,该问题可能与主题不兼容、资源未正确定义或样式冲突有关[^1]。 以下是一些可能导致此问题的原因及解决方案: #### 1. 主题不兼容 如果Activity的主题不是`Theme.AppCompat`或其子类,则可能会导致`InflateException`。这是因为某些控件(如`TextView`)依赖于`AppCompat`主题中的特定属性[^1]。 确保在`AndroidManifest.xml`中为相关Activity设置正确的主题: ```xml <activity android:name=".YourActivity" android:theme="@style/Theme.AppCompat.Light"> </activity> ``` #### 2. 资源类型错误 错误消息提到`Resource is not a Drawable (color or path)`,这表明某个资源被错误地引用为Drawable,而实际上它并不是。检查布局文件中的`TextView`是否引用了无效的资源。例如,以下代码可能导致问题: ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@string/some_string" /> ``` 上述代码中,`@string/some_string`被错误地用作背景资源。应改为有效的Drawable资源或颜色值: ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/your_color" /> ``` #### 3. 样式冲突 虽然引用[2]提到默认的`TextView`样式通常不会有问题,但如果自定义了样式,可能会引入冲突。确保自定义样式的父类是`TextAppearance.AppCompat`或类似的兼容样式[^2]。例如: ```xml <style name="CustomTextStyle" parent="TextAppearance.AppCompat"> <item name="android:textColor">@color/text_color</item> <item name="android:textSize">16sp</item> </style> ``` 然后在布局中应用该样式: ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/CustomTextStyle" /> ``` #### 4. 版本兼容性 如果使用的是较新的AndroidX库,而项目中仍存在旧版支持库的引用(如`android.support.design.internal.NavigationMenuView`),可能会导致兼容性问题。建议将所有支持库迁移到AndroidX,并确保Gradle文件中版本一致: ```gradle implementation &#39;androidx.appcompat:appcompat:1.4.1&#39; implementation &#39;com.google.android.material:material:1.6.0&#39; ``` #### 5. 检查日志中的具体行号 错误消息中提到`Binary XML file line #17`,这表示问题出现在XML文件的第17行。仔细检查该行及其附近的代码,确保没有无效的资源引用或语法错误。 --- ### 示例代码 以下是一个完整的布局文件示例,展示了如何避免常见的`InflateException`问题: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/my_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World" android:textColor="@color/black" android:background="@drawable/background_shape" /> </LinearLayout> ``` 同时,确保`res/drawable/background_shape.xml`文件存在且格式正确: ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/white" /> </shape> ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值