I tried to set background for LinearLayout, I used code setBackgroundResource() but it has error. Please help me, thanks.
Asked
Active
Viewed 7,914 times
-5
-
2Post your XML file. – Jaymin Mar 27 '19 at 08:42
-
Possible duplicate of [Android resource not found exception?](https://stackoverflow.com/questions/7727808/android-resource-not-found-exception) – Manoj Perumarath Mar 27 '19 at 08:51
2 Answers
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,241
- 1
- 13
- 24
-
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
-
1From 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
-
1In 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
0
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,041
- 5
- 25
- 43