Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText cannot be resolved to a type

Tags:

android

I have defined a layout in an xml file in the 'res' folder of my android project. The 'EditText' element looks like:

  <EditText android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numeric="integer|decimal"></EditText>

In my class file in my android project, I have the following:

public void doCalculation(View view) {
        String firstNo = ((EditText) findViewById(R.id.editText1)).getText().toString();
        String secondNo = ((EditText) findViewById(R.id.editText2)).getText().toString(); 
        String operator = ((Spinner) findViewById(R.id.spinner1)).getSelectedItem().toString();
        callWebService(firstNo, secondNo, operator );
}

Unfortunately, the first 2 assignments in my method above are showing an error in eclipse stating

EditText cannot be resolved to a type

I've no idea how to fix this. I'm using android 2.3.3 API 10. Any help would be appreciated. Thanks

like image 715
Joeblackdev Avatar asked Jul 23 '11 11:07

Joeblackdev


4 Answers

You need to import the EditText class, so it's known, using the following line at the beginning of your .java file :

import android.widget.EditText;


Note that, in most cases, Eclipse can help you a lot : it has an Organize Imports feature, that will add the required import lines :

  • Menu > Source > Organize Imports
  • Or use Ctrl + Shift + O
like image 163
Pascal MARTIN Avatar answered Oct 18 '22 05:10

Pascal MARTIN


Did you try adding this manually?

import android.widget.EditText;

Also check your console & error log for additional errors. Usually with things this obvious the reason can be something else too.

If the import doesnt work, try closing and reopening your project.

like image 44
Ophidian Avatar answered Oct 18 '22 04:10

Ophidian


If you tried to ad import android.widget.EditText, and doesn't worked try to clean your project at Project -> Clean... and Try to click with right mouse button on your project choose Android tools then fix project properties. Hope it helps.

like image 32
Zwiebel Avatar answered Oct 18 '22 03:10

Zwiebel


If none of the other answers work, you can always do this:

EditText txt1 = (EditText)findViewById(R.id.editText1);
EditText txt2 = (EditText)findViewById(R.id.editText2);

String firstNo = txt1.getText().toString();
String secondNo = txt2.getText().toString();
like image 45
Rob Avatar answered Oct 18 '22 05:10

Rob