0

The android R has been troubling me for quite some time now. I have seen it in a lot of projects of mine, sometimes they just disappear, sometimes I have had to recreate a project, etc. Still this does not solve anything for my current project.

Logically I suspect the XML files to contain an error but I am unable to find it.

I am using Eclipse as my development environment and this is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/title"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btnNewTime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/overview" />

    <Button
        android:id="@+id/btnOverview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/overview" />

    <Button
        android:id="@+id/btnValues"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/values" />

    <Button
        android:id="@+id/btnExit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit" />

</LinearLayout>

This is my strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Werkregistratie</string>
    <string name="title">Werkregistratie</string>
    <string name="overview">Overzicht</string>
    <string name="values">Salaris/reiskosten aanpassen</string>
    <string name="exit">Afsluiten</string>
</resources>

I am unable to find the problem so I really hope you can help me out.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
Mark Pross
  • 45
  • 1
  • 1
  • 7
  • Did you try to simply clean the project? – Egor Jul 08 '12 at 18:59
  • yea of course i did, i've cleaned it, tried the fix project properties, tried to remove the gen and even the src from the buildpath and re-add them. None of those work – Mark Pross Jul 08 '12 at 19:03
  • Your XML files look fine. Try to import one of Android sample apps to your workspace and build it, maybe there's an issue with your development environment. – Egor Jul 08 '12 at 19:05
  • hmm other projects have the same issue, what could the the issue be inside the dev environment? I'm using the SDK 15(android 4.0.3) which is also the target SDK, got the android itself(with tools/platform tools) installed – Mark Pross Jul 08 '12 at 19:08
  • Have you checked if there are any errors or warnings in the Problems view? Go to Window > Show View > Problems. Also check if your Android SDK and plugin are the same by updating both. In rare cases I needed to delete an issue from the Problems view manually before it would re-genarate anything. – Michiel Jul 08 '12 at 19:10
  • I have checked it and the only error it outputs is the fact that the R class cannot be found – Mark Pross Jul 08 '12 at 19:31

6 Answers6

1

Ok this seems to be just a bug in eclipse, quite some answers are partially correct. Make sure the names validate(you can use Lint in some cases). Make sure the xmls validate. Then check your buildpath for errors. After that do a project clean and all should be fine.

Also, this problem doesn't appear as much in the new android sdk's so they fixed something it seems.

Mark Pross
  • 45
  • 1
  • 1
  • 7
0

If your package name is "com.example.test"

then try something like this-

findViewById(com.example.test.R.id.textview1);

not like this-

findViewById(R.id.textview1);

(* Only use when your R file is not generated)

  • 1
    Dude he has to change his whole project which accordingly is not an efficient way. He has to go with a proper way and generate R file . – LuminiousAndroid Jul 08 '12 at 19:20
  • still this doesn't fix the problem, it still gives the same error when using that, it just can't find the R class at all – Mark Pross Jul 08 '12 at 19:29
  • Except this you also need to import - import com.example.test.R; –  Jul 08 '12 at 20:29
0

I tried your xml and its work well for me , Hopefully your java class code might have some problem. Try to find out problem inside your and surely clean the project. All the best :)

LuminiousAndroid
  • 1,557
  • 4
  • 18
  • 28
0

If your package name is com.foo.bar.package, then you need to import it like this:

import com.foo.bar.package.R;

Then the findViewById should work using this notation:

Button btnValue = (Button)findViewById(R.id.btnValue);

Sometimes, Eclipse can be quite obnoxious when it comes to that "Resources missing" a lá cannot find the R file which does not get generated.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • That does not sound right there! In your Eclipse project's package explorer, there should be, project > `src/`, `gen/` (*which is where the R gets generated*), `res/layout` (*xml layouts go into*), `res/values` (*strings go in there*). The reason the import cannot find the R either is something went screwy in there with Eclipse.. have you not thought of using `ant` and do the `ant clean` on the command line, then try build the project from eclipse again? – t0mm13b Jul 08 '12 at 19:40
  • ok this is getting interesting, ant gives me this output: BUILD FAILED /usr/android-sdk-linux/tools/ant/build.xml:377: Problem: failed to create task or type checkenv Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any / declarations have taken place. – Mark Pross Jul 08 '12 at 19:48
  • usually, `android update project -p . -t x` where x is the sdk level, will generate enough of a build.xml which would be sufficient enough to carry out the `clean`. Interesting... – t0mm13b Jul 08 '12 at 20:27
0

I just faced this issue today when importing an app src from workspace. all line of code using R was showing error.

I read somewhere that I should actually set tagetSDK as 4.0 from properties/android view. and minsdk version as 8 in manifest.

It worked like charm.

One more thing it could be the issue with any res file/layouts/drawable have the names with capital letter. Some time Thumbs.db got generated in drawable folder and cause the same issue.

Thanks!!!

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • i'll try the import later, but i use a minsdk of 8 as well and a max sdk of 16(android 4.1) and that doesn't fix it, since i use linux Thumbs.db is a really uncommon thing but i'll check for capital filenames – Mark Pross Jul 08 '12 at 22:30
0

I think I've ran into this problem before, the only way I could get around it is to try again with a fresh project and import my code. Inefficient and clunky I know but this was how I managed to get the R file to generate after a frustrating few hours.

Failing that you might be able to find something to help you here

Community
  • 1
  • 1
Darryl Bayliss
  • 2,677
  • 2
  • 19
  • 28