9

Just added a new button to my already-working-fine layout, but the findViewById function seems to be angry with something I don't get to understand.

Here's a bit of the layout:

<LinearLayout
        ...
    >
    <ListView
        android:id="@+id/my_lovely_list"
        android:layout_width="fill_parent"
        android:layout_weight="1" 
        />

    <Button
        android:id="@+id/my_lovely_butt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit_b"
        android:layout_weight="0"
        android:clickable="true"
        />

</LinearLayout>

And here's a bit of the coding:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ...
    list_o = (ListView)findViewById(R.id.my_lovely_list);
    butt_o = (Button)findViewById(R.id.my_lovely_butt);
    ...
}

So, the big mistery is that the ListView is found without any problem, but the Button won't by any means. I've already tried cleaning the Proyect, and look throught the posts I've found here... but still don't get to find the problem! Any thoughts?

DrKiss
  • 609
  • 2
  • 5
  • 12
  • i think reference is not creating in R.java file. Delete the button & create it again.... – code_finder Jul 13 '12 at 09:31
  • **"but the Button won't by any means."** - What are you getting? any **NullPointerException**? – user370305 Jul 13 '12 at 09:55
  • Close Eclipse once and restart again. I think this will solve your problem. – Nirali Jul 13 '12 at 09:32
  • I just can't build the project.. I get the error "my_lovely_butt cannot be resolved or is not a field" – DrKiss Jul 13 '12 at 11:52
  • well, now it works... but I have no idea why, I've tried everything that was advised to me (excluding to go back to a previous working state and code everything again), nothing worked... I went to do something else and when I got back to the computer, the error was gone... the helping little gnomes, perhaps? we'll never know... – DrKiss Jul 13 '12 at 14:31
  • 2
    "My lovely butt" ‒ sorry for spamming :-) –  Jul 13 '12 at 21:06

6 Answers6

21
import yourpackagename.R;

instead of android.R;

import R of your package

Also Clean your project that will refresh your entire project then you will also find ID of button also

MAC
  • 15,799
  • 8
  • 54
  • 95
  • 4
    `android.R` doesn't contain any id **my_lovely_list**. So if this was the case, he wasn't even able to build the app. – waqaslam Jul 13 '12 at 09:37
6

Some times the file R is not regenetated. Try :

  • Clean your project
  • Rebuild it
  • Manage to go back to a version that compiled and retry adding your elements one by one and compile each time to check the R file
  • Finnally the last option is to close/reopen eclipse yep some bugs can't be overcome other way ^^"

No worry that's is just some common eclipse/android bug it happen often (Well some other times it can also be a small syntax error somewhere in your project but i assume that you already checked that x)

Good luck

An-droid
  • 6,433
  • 9
  • 48
  • 93
4
  1. Clean your project in Eclipse.
  2. Make sure the id is not duplicated
  3. Remove the button, save and add the button again and build the code.
  4. Make sure the correct R file is imported.

If its a syntax related problem, then its got to do with compile time stuff.

If its a problem when the code executes, make sure the layout is inflated. If the listview is found, then the button also must definitely be found.

gaara87
  • 2,087
  • 3
  • 21
  • 43
3

Make sure to go through these processes.

1: Make sure you imported your project R file.

import packagename.R

2: Clean your project

enter image description here

3: If the above two solutions doesn't work, do invalidate your cache and restart enter image description hereenter image description here

1

In Android Studio Build -> Clean Project resolved the problem for me.

Rocky Inde
  • 1,511
  • 1
  • 20
  • 27
0

while, just by clean you project, rebuild it, or import the R file manually, it might a sad thing to find the id of specified view component still cannot find by the activity. and then, if you check the detailed content of the R file, you will find the id has not generate, and then, what you need to check, if there are something wrong with your own /res file, for example, the name of the layout file, drawable file name, or the variant name of the menu file .etc. all of these could just make the R file cannot generate correctly, or even worse, the R file does not generate out at all.