Home » Android » android – FindViewById() not finding View

android – FindViewById() not finding View

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:
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

Answer:

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

Answer:

  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.

Answer:

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

Answer:

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

Answer:

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.