110

Please let me know where I am going wrong to get the error.

I am creating an app which have one of its activity to be only in landscape mode. So I added the following in AndroidManifest.xml file

<activity android:name=".LandScapeImageActivity" android:screenOrientation="landscape"></activity>

I have created a folder like

/res/layout-land

and add a layout called see_today_landscape_layout in it.

and in onCreate() I added the following

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.see_today_landscape_layout);
....
}

But when I run my app I am getting the following error

02-06 13:46:14.358: E/AndroidRuntime(13286): FATAL EXCEPTION: main
02-06 13:46:14.358: E/AndroidRuntime(13286): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mid.kew.activities/com.mid.kew.activities.LandScapeImageActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f03002b
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4066)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.ActivityThread.access$2400(ActivityThread.java:135)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2140)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.os.Looper.loop(Looper.java:144)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.ActivityThread.main(ActivityThread.java:4937)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at java.lang.reflect.Method.invokeNative(Native Method)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at java.lang.reflect.Method.invoke(Method.java:521)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at dalvik.system.NativeStart.main(Native Method)
02-06 13:46:14.358: E/AndroidRuntime(13286): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f03002b
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.content.res.Resources.getValue(Resources.java:892)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.content.res.Resources.getLayout(Resources.java:731)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.Activity.setContentView(Activity.java:1654)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at com.mid.kew.activities.LandScapeImageActivity.onCreate(LandScapeImageActivity.java:103)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
02-06 13:46:14.358: E/AndroidRuntime(13286):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
02-06 13:46:14.358: E/AndroidRuntime(13286):    ... 12 more

I crossed checked in R.java and it has the resource with the ID 7f03002b which the exception is looking for and it is present in there...

I cleaned and rebuild the project for say 5 times, but still the issue occurs.

Strange point is that this was working yesterday and it's not working today. The code is the same.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Nik
  • 2,913
  • 7
  • 40
  • 66
  • What does Resource ID #0x7f03002b refer to? You should provide the content of `see_today_landscape_layout.xml` file. – a.ch. Feb 06 '12 at 14:55
  • yes its refering to see_today_landscape_layout.xml as per R.java – Nik Feb 06 '12 at 15:07
  • 1
    Possible duplicate of [Android resource not found exception?](http://stackoverflow.com/questions/7727808/android-resource-not-found-exception) – rds May 09 '16 at 12:25
  • I had this issue when upgrading to use `compile 'com.android.support:appcompat-v7:23.4.0'` if I stay with `v7:22.2.0'` it works – tread May 14 '16 at 15:15

16 Answers16

239

For my condition the cause was taking int parameter for TextView. Let me show an example

int i = 5;
myTextView.setText(i);

gets the error info above.

This can be fixed by converting int to String like this

myTextView.setText(String.valueOf(i));

As you write int, it expects a resource not the text that you are writing. So be careful on setting an int as a String in Android.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Yekmer Simsek
  • 4,102
  • 3
  • 21
  • 19
  • Thanks, it worked, I was trying to do like this: num2.setText(lotteryCharacters.charAt(2));, then I changed to num2.setText(String.valueOf(lotteryCharacters.charAt(2)));...and it worked! – TharakaNirmana Jun 08 '13 at 14:41
  • 1
    I wonder what's the difference between valueOf(i) and "" + i? – peterb May 16 '16 at 01:00
  • 1
    How about XAML code, how can I set the value. My case is should set decimal value for property on XAML – zquanghoangz Dec 07 '16 at 09:33
  • 3
    Thanks, the reason is that setText is overloaded method that accepts both String and resource Id. Then you pass an int it looks for resource Id – abedfar Apr 21 '17 at 08:09
  • I had the same error with setting a char as a text to textview. This fixed it. – marts Aug 07 '18 at 08:04
38
  1. in eclipse, go to Project > Clean...
  2. select your project, then press OK
  3. relaunch the app

if it happens again delete the r.java file. it will generate automatically.

Falko
  • 17,076
  • 13
  • 60
  • 105
prakash
  • 644
  • 4
  • 21
  • 2
    as I mentioned before... I did clear bin and gen folders and rebuild the app.. still not working... – Nik Feb 06 '12 at 15:13
  • 160
    If this didn't fix your problem, why did it get the check mark? – IcedDante Feb 05 '14 at 21:09
  • 2
    what do you do if this happens on the release app? – Dinesh Sep 03 '19 at 20:38
  • It worked for me in Intellij. I was getting this exception for only release builds, but after Build -> Clean Project -> Restart IDE, my release build started to run as it should. – chirayu joshi Jun 19 '23 at 15:34
14

Since you are setting the layout explicitly you might want to try and put it in the default /layout folder not in the /layout-land since that is if you want Android to automatically handle rotation for you.

mbwasi
  • 3,612
  • 3
  • 30
  • 36
  • 1
    If the phone is in portrait view it will be in portrait, when you rotate the phone it will be in landscape. Are you trying to make it always landscape? Then what you need to do is set the orientation of the activity to landscape in the Manifest file or in code see this Question http://stackoverflow.com/questions/2150287/force-an-android-activity-to-always-use-landscape-mode – mbwasi Feb 06 '12 at 15:21
  • I have done that as I have mentioned in my question '' – Nik Feb 06 '12 at 15:23
  • 1
    you are missing the configchanges part – mbwasi Feb 06 '12 at 16:05
11

Try moving your layout xml from res/layout-land to res/layout folder

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Ashish Kumar
  • 409
  • 1
  • 4
  • 12
4

I just get this error on Android 6. I moved all drawables from drawable-v24 to drawable folder and the error is finally gone.

Nasser Ghodsian
  • 214
  • 3
  • 3
2

Check to make sure that your imports are correct. I had a similar problem where R was pointing to the Android system R file, not my local one.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
2

This can happen due to a different language in the phone for which your code doesn't have the asset for. For example your preference.xml is placed in xml-en and you are trying to run your app in a phone which has French selected, the app will crash.

Arslan Mehboob
  • 1,012
  • 1
  • 9
  • 21
  • Always create default asset folders and then add language or orientation specific ones. Although this might or might not be the case over here :) – Ahmad Ali Nasir Nov 05 '14 at 06:11
2

I got this error from yet another reason:

I had the file res/xml/data.xml and I was trying to load it with Resources class like this:

Resources.getSystem().getXml(R.xml.data);

However this is incorrect as the method Resources.getSystem() returns a global shared Resources object that provides access to only system resources.

The correct way is as follows (from inside Activity):

this.getResources().getXml(R.xml.data);
astasiak
  • 790
  • 1
  • 6
  • 13
2

In my case my Build Tools version in my build.gradle for the app module was outdated on an old project. Updating it fixed the issue:

android {
    ...
    buildToolsVersion "19.0.1"
    ...

Updated to the latest build tools version (25.0.1) and sync'd the project and all was well again.

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
1

I deleted folders build inside a project. After cleaned and rebuilt it in Android Studio. Then corrected errors in build.gradle and AndroidManifest.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
1

This can also cause some trouble: Accidentally one of my layouts was parked in my tablet resources folder, so I got this error only with phone layout. The phone layout simply had no suitable layout file.

I worked again after moving the layout file in the correct standard folder and a following project rebuild.

wolwe
  • 11
  • 2
0

Make sure that the R you are pointing to is the correct one. I had a problem very similar to this, where an import got inserted by Eclipse that pointed to the System R file rather than the project one. It took a lot of head scratching. Hope this helps.

Rben
  • 489
  • 1
  • 7
  • 18
0

You have screen orientation set to landscape in your xml. If you have used eclipse to generate this file it would have created under res/layout-land/ folder.But when you open activity in Portrait mode application will search for xml in res/layout-port/ folder or the regular res/layout/. If you didn't have xml for portrait mode your application will crash.

Prakash
  • 7,794
  • 4
  • 48
  • 44
0

I have fixed the by this way:

Create a folder in your resource directory name "drawable-nodpi" and then move yours all resources in this directory from others drawable directory.

Now clean your project and then rebuilt. Run again hopefully it will work this time without any resource not found exception.

0xAliHn
  • 18,390
  • 23
  • 91
  • 111
  • This pointed me to the right track but please keep in mind this post: https://stackoverflow.com/questions/24682094/will-a-density-qualified-drawable-folder-or-drawable-nodpi-take-precedence – Adrian Aug 05 '19 at 08:23
0

I got this error when I was trying to access Bundle data from One Intent by using getInt("ID").

I solved it by using getString("ID").

From Activity1 i had

Intent intent=new Intent(this,ActivityB.class);
intent.putExtra("data",data)// 
startActivity(intent);

On Activity B,

Bundle bundle=getIntent().getExtras();
if(extras!=null){
// int x=extras.getInt("Data"); This Line gave me error int 
x=Integer.parseInt(extras.getString("Data")); // This solved the problem.
}
  • 1
    Please update your answer to explain how this solves the problem, and improves on the upvoted and accepted answers this 5-year old question already has. See: [How do I write a good answer](https://stackoverflow.com/help/how-to-answer). – FluffyKitten Oct 11 '17 at 16:07
  • Please consider adding some details and explanation to support your answer so it is helpful to others – Souvik Ghosh Oct 11 '17 at 16:18
0

For me cache invalidation and restart didn't work. I removed the .idea and .gradle folders. If you do that, don't forget that things like build logs or something might go away though.

Bastian Springer
  • 271
  • 2
  • 11