FindViewById () cannot find View

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

Here is a little 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 is some code:

 @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 problem is that the ListView is found without any problems, but Button will not be in any way. I already tried to clear Proyect and see all the messages that I found here ... but still not find this problem! Any thoughts?

+6
source share
5 answers
 import yourpackagename.R; 

instead of android.R;

import R your package

Also Clean your project, which will update your entire project, you will also find button ID as well

+16
source

The R file is not restored several times. Try:

  • Clean up project
  • Restore it
  • Control the return to the version that compiled and repeats adding your items one at a time and compiles each time to check the R file
  • The final option is closing / reopening the eclipse. Some errors cannot be overcome in another way. ^^ "

Don’t worry that this is just a regular eclipse / android error that happens often (well, sometimes in other cases it might be a small syntax error somewhere in your project, but I assume you already checked that x)

Good luck.

+6
source
  • Clean up the project in Eclipse.
  • Make sure the identifier is not duplicated.
  • Delete the button, save and add the button again and create the code.
  • Make sure the correct R file is imported.

If this is a syntax issue, then this is about compiling time.

If this is a problem while executing the code, make sure the layout is overpriced. If the list is being browsed, a button must also be found.

+3
source

while just cleaning up the project, rebuilding it, or importing the R file manually, it may be sad that the identifier of the specified component of the view still cannot be found by activity. and then if you check the detailed contents of the R file, you will find that the identifier is not generated, and then you need to check if something is wrong with your / res file, for example, the name of the layout file, the name of the file with the ability to draw or the name of the .etc menu file option. all this can lead to the fact that the R file cannot be created correctly or, even worse, the R file is not generated at all.

0
source

Android Studio Build Clean Project resolved the issue for me.

0
source

Source: https://habr.com/ru/post/920398/


All Articles