Android添加第二个ImageView后应用崩溃问题求助
解决ImageView导致的Android应用崩溃问题
嘿,我仔细看了你的布局代码和崩溃日志,马上就定位到了问题根源!
核心崩溃原因
从日志里的这条关键错误信息就能看出问题:
Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.animation:drawable/chandelier" (7f060064) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060064 a=-1 r=0x7f060064}
简单来说,你引用的@drawable/chandelier这个资源有问题——它要么不存在,要么不是Android能识别的Drawable类型(比如图片格式不对、放错了文件夹,或者文件名拼写错了)。
一步步修复方案
检查Drawable资源的正确性
- 先确认
chandelier对应的图片文件(比如png、jpg或者矢量图xml)确实在res/drawable(或者对应的密度文件夹,比如drawable-hdpi)目录里 - 注意Android资源文件名是大小写敏感的!别把
chandelier写成Chandelier或者其他拼写错误 - 如果是矢量图,要确保它放在
res/drawable目录下,并且你的项目配置了支持矢量图(低版本Android需要额外配置,后面会说)
- 先确认
修正布局里的小问题
你的布局里还有两个小瑕疵,虽然不是这次崩溃的直接原因,但留着可能会出别的问题:- RelativeLayout不需要
android:orientation属性,这个是LinearLayout专属的,删掉它就行 - XML里不能用
/////这种注释,要改成标准的<!-- 注释内容 -->格式
给你贴个修正后的完整布局代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".guidance_new" android:id="@+id/layout" android:background="@drawable/bg_animation" android:padding="16dp"> <ImageView android:layout_width="75dp" android:layout_height="75dp" android:id="@+id/hanging_lamp" android:src="@drawable/bulb1"/> <LinearLayout android:id="@+id/linear1" android:layout_width="match_parent" android:layout_height="180dp" android:orientation="vertical" > <TextView android:layout_width="150dp" android:layout_height="70dp" android:text="100W Bulb" android:textColor="#000" android:textStyle="bold" android:textSize="20sp" android:layout_marginLeft="180dp" android:id="@+id/textView1"/> <TextView android:layout_width="150dp" android:layout_height="80dp" android:text="Price:100/. Voltage:220V-240V ." android:textColor="#a9a9a9" android:textStyle="bold" android:textSize="20sp" android:layout_marginLeft="180dp" android:id="@+id/textView2"/> <!-- layout is set here --> </LinearLayout> <ImageView android:layout_width="75dp" android:layout_height="75dp" android:id="@+id/chandlier" android:src="@drawable/chandelier" android:layout_below="@id/linear1"/> <LinearLayout android:layout_width="match_parent" android:layout_height="180dp" android:layout_below="@id/linear1"> <TextView android:layout_width="150dp" android:layout_height="70dp" android:text="Chandlier" android:textColor="#000" android:textStyle="bold" android:layout_marginLeft="180dp" android:textSize="20sp"/> </LinearLayout> </RelativeLayout>- RelativeLayout不需要
清理缓存并重建项目
有时候Android Studio会缓存错误的资源引用,做下面这几步大概率能解决:- 点击顶部菜单栏的
Build > Clean Project - 然后点击
Build > Rebuild Project - 要是还不行,重启一下Android Studio,缓存问题经常这么解决
- 点击顶部菜单栏的
额外小贴士
如果你的chandelier是矢量图,为了让低版本Android系统(API 21以下)也能正常显示,记得在app/build.gradle的android块里加这段配置:
android { defaultConfig { vectorDrawables.useSupportLibrary = true } }
内容的提问来源于stack exchange,提问作者Aj Styles
相关产品推荐
相关产品推荐

