0

so I'm trying to learn Android Studio and this is the first time I've done anything. I'm trying to make a simple login app and I just added two EditText fields but they are not being recognized.

I get this error;

Cannot resolve symbol EditText

Although I made sure I have an EditText field in my application.

This is the Java code:

package localhost.testloginphpapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

EditText usernameField, passwordField;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   }
}

And the XML code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="localhost.testloginphpapp.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">

<EditText
    android:id="@+id/usernameField"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Username"
    tools:layout_editor_absoluteX="85dp"
    tools:layout_editor_absoluteY="133dp" />

<EditText
    android:id="@+id/passwordField"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword"
    tools:layout_editor_absoluteX="85dp"
    tools:layout_editor_absoluteY="190dp" />

<Button
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    tools:layout_editor_absoluteX="148dp"
    tools:layout_editor_absoluteY="245dp" />

</android.support.constraint.ConstraintLayout>

Thanks a lot.

user6613235
  • 49
  • 1
  • 10

2 Answers2

2

You have to write this line of code under the package but before the class:

import android.widget.EditText;
Curio
  • 1,331
  • 2
  • 14
  • 34
  • I will try that soon. Thanks. Do I have to import anything I want to use? Say a button, etc. – user6613235 Apr 25 '17 at 20:23
  • Yeah are you developing in Android studio? normally it should tell you what to import – seven_seas Apr 25 '17 at 20:24
  • @user6613235 normally it's automatic, otherwise import in this way – Curio Apr 25 '17 at 20:26
  • @BAAAZINGA I am but it didn't tell me anything. You said something about having to initialize before anything gets added? How to do that if I may ask? – user6613235 Apr 25 '17 at 20:27
  • 1
    Okay weird normally it should underline it red and tell you to import That was about something different i assumed you worked with _EditText usernameField, passwordField;_ later in the Code that would've caused this error because you would try to interact with an empty variable – seven_seas Apr 25 '17 at 20:35
  • @Curio is there a way for me to make it automatic? Is it an option that I have to enable or just a bug? – user6613235 Apr 25 '17 at 20:49
  • @user6613235 see here http://stackoverflow.com/questions/16615038/what-is-the-shortcut-to-auto-import-all-in-android-studio – Curio Apr 26 '17 at 21:06
0

EditText is a widget. You can use it in XML. But in Java, You have to import its class by adding

android.widget.EditText

on top of the file before class declaration.