You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:LinearLayout与CollapsingToolbarLayout背景图报错求助

Troubleshooting Background Image Error in LinearLayout & CollapsingToolbarLayout

Hey there! Let’s fix that @drawable/img background error you’re hitting as an Android beginner. This issue almost always boils down to a few common missteps—let’s walk through each one to get your images showing up correctly.

Common Causes & Fixes

1. Double-check your image resource exists

  • Make sure you actually have an image named img (exact spelling, case-sensitive!) in your res/drawable folder. Android treats Img.png and img.png as totally different files, so watch those capital letters.
  • Stick to supported formats: PNG, JPG, or WebP work best. If you’re using an SVG, you’ll need to place it in drawable-v24 or use VectorDrawableCompat for backward compatibility.

2. Verify your XML reference is correct

  • For LinearLayout, the background should be set like this (no typos!):
    android:background="@drawable/img"
    
  • For CollapsingToolbarLayout, you might be using app:contentScrim or an ImageView as the parallax background. Don’t forget to declare the app namespace at the top of your layout file:
    xmlns:app="http://schemas.android.com/apk/res-auto"
    
    Here’s a valid CollapsingToolbarLayout setup with an image:
    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="280dp"
        app:contentScrim="@drawable/img"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
        <!-- Parallax background image -->
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/img"
            app:layout_collapseMode="parallax"/>
    
        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"/>
    
    </com.google.android.material.appbar.CollapsingToolbarLayout>
    

3. Clean & rebuild your project

Android Studio’s resource cache can sometimes glitch out. Head to the top menu:

  • Click Build > Clean Project
  • Then Build > Rebuild Project
    This forces Studio to reprocess all your resources, which often fixes "missing" resource errors.

4. Ensure your filename follows Android rules

Resource filenames can only use lowercase letters, numbers, underscores, and dots. No spaces, uppercase letters, or special characters like hyphens. For example, img_hero.png is okay, but Img Hero.png is not.

If you’re still stuck...

Check the Logcat tab for the full error message (look for ResourceNotFoundException). It’ll tell you exactly which resource can’t be found, which helps narrow down the issue even more.

内容的提问来源于stack exchange,提问作者Nanda Febrian A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:15:16