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 = ((S开发者_如何转开发pinner) 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
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
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.
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.
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();
Check for imports.
To get the text from EditText
try get the TextView
value. It might work.
String firstNo = ((TextView) findViewById(R.id.editText1)).getText().toString();
String secondNo = ((TextView) findViewById(R.id.editText2)).getText().toString();
Above your class, just import:
import android.widget.EditText;
精彩评论