29953

findViewById Error cant find id in XML

Question:

I cant find out why my findViewById is not linking to the XML file and it is driving me nuts. The is is in the XML file but it is still not Finding it in the java MainActivity file.

here is my XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/editCentimeters" android:layout_width="wrap_content" android:layout_height="27sp" android:layout_alignLeft="@+id/editInches" android:layout_alignTop="@+id/textCentimeters" android:ems="5" > <Button android:id="@+id/buttonConvert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editInches" android:layout_centerHorizontal="true" android:layout_marginTop="98dp" android:text="@string/convert" />

And here is my Java

EditText etCentimeters = (EditText) findViewById(R.id.editCentimeters); Button buttonConvert = (Button) findViewById(R.id.buttonConvert);

Answer1:

I was facing the same issue. Just comment out the line which is throwing the error & BUILD the project once. Intellisense (<kbd>ctrl</kbd> + <kbd>space</kbd>) then will show you the IDs.

Answer2:

You need to take care of 2 things here:-

EditText etCentimeters = (EditText) findViewById(R.id.editCentimeters); Button buttonConvert = (Button) findViewById(R.id.buttonConvert);

Firstly, the above code should be present in the onCreate() method.

And next, you need to place that code snippet below this line in the onCreate() method, not before it.(I'm assuming that the setContentView is already present, if not, add that)

setContentView(R.layout.main);

Answer3:

use setContentView(R.layout.main); at the onCreate of your Activity

Answer4:

You can refresh project. And also check ur setContentView(R.layout.YOURLAYOUT_NAME) in Your activity. Make sure that u are using ur code in activity.

Answer5:

Just clean & build in order to find the ID.

Recommend

  • My Android app crashes after adding “@android:style/Theme.Dialog” in the AndroidManifest.xml
  • App is crashing when we upload the image from gallery?
  • Android set navigation drawer list to open exact half of the screen for all device screen
  • How we can put Floating action button on top of any layout
  • sometimes line doesn't shows on the graph- a chartengine
  • How can I fill an ImageView in an Appwidget while mainiting the aspect ratio?
  • How can i make my video view to half the screen of my device?
  • Android: How to keep corner radius while setting a background drawable?
  • Android border bottom for custom view not working
  • spatie/laravel-analytics performQuery error
  • How to make a reader app where text should adjust in multiple pages according to screen size and fon
  • How to set my android layout in all devices?
  • Android Textview text cut off at bottom
  • Layout for Fragment overlapping Toolbar
  • How insert a LinearLayout between AppBarLayout and the scrolling content, when using CollapsingToolb
  • FloatingActionButton visible for sometime even if visibility is set to gone
  • ImageSwitcher is not showing outAnimation
  • How to have an EditText stuck to the soft Keyboard on Android
  • Multiple Intents on Main Menu
  • adMob | Extend Banner size?
  • Custom auto hide floatingActionButton behavior is not working
  • Setting attributes of an EditText added dynamically in Android
  • Update Search Results to Lazy Adapter in android
  • Android onKey w/ virtual keyboard
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Javascript Callbacks with Object constructor
  • How to set my toolbar fixed while scrolling android
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?