kevincrab
kevincrab

Reputation: 74

findViewById Error cant find id in XML

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);

Upvotes: 0

Views: 8766

Answers (6)

Code Wiget
Code Wiget

Reputation: 1672

Clean & build didn't work for me. I had to restart Android Studio.

Upvotes: 0

Sebastian Paduano
Sebastian Paduano

Reputation: 361

Just clean & build in order to find the ID.

Upvotes: 0

Fusion
Fusion

Reputation: 175

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

Upvotes: 8

Dinakar Prasad Maurya
Dinakar Prasad Maurya

Reputation: 371

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.

Upvotes: 0

Lebedevsd
Lebedevsd

Reputation: 950

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

Upvotes: 0

Rahul
Rahul

Reputation: 45060

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);

Upvotes: 0

Related Questions