문제

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);
도움이 되었습니까?

해결책

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.

다른 팁

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

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

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.

Just clean & build in order to find the ID.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top