问题
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);
回答1:
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.
回答2:
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);
回答3:
use setContentView(R.layout.main);
at the onCreate of your Activity
回答4:
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.
回答5:
Just clean & build in order to find the ID.
回答6:
Clean & build didn't work for me. I had to restart Android Studio.
来源:https://stackoverflow.com/questions/18871959/findviewbyid-error-cant-find-id-in-xml