-5

enter image description here

enter image description here

I tried to set background for LinearLayout, I used code setBackgroundResource() but it has error. Please help me, thanks.

user1767316
  • 3,276
  • 3
  • 37
  • 46

2 Answers2

5

Try to clean and rebuild your project.

If that doesn't work, use this code to set the background

linearLayout.setBackground(ContextCompat.getDrawable(this, R.drawable.img1))

By the way, next time you should post the code directly instead of passing in a link.

Harvey
  • 1,353
  • 1
  • 14
  • 27
  • It doesn't work for me, The program is still error,thank for you reminder – Nguyễn Hữu Đại Mar 27 '19 at 09:12
  • 1
    From the first image you attached, I can see that the img1.jpg file is stored in drawable-v24 folder. It should be stored in drawable folder. Move the img1.jpg to drawable folder and your problem will be gone. – Harvey Mar 27 '19 at 17:44
  • 1
    In case you're new to Android Studio, click on the ```Android option``` on the top of Project tab and change it to ```Project```. You will see a bunch of drawable folders in res folders. – Harvey Mar 27 '19 at 17:47
  • Thanks bro,I moved the img1.jpg to drawable folder. My problem has repaired – Nguyễn Hữu Đại Mar 28 '19 at 01:04
1

This happens common when you add the resource to another locale, let's say you have two locales, one is fr (French) and the other is the default en. When you put the image resource in the fr locale and run the app with the default locale, you will see this crash.

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43