Alexandru Mihai Alexandru Mihai - 1 month ago 6
Android Question

android-can't get value of edit text in fragment

I've tried get edittext value but does not work.This is my code in android fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v=inflater.inflate(R.layout.fragment_add_patient, container, false);
final EditText FirstName=(EditText) v.findViewById(R.id.FirstName);
final Button addPatientButton=(Button) v.findViewById(R.id.AddPatientButton);
addPatientButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isOnline()) {
try {
if (FirstName.getText().toString().trim().equals("")) {
FirstName.setError("First Name is required!");
return;
}


I receive error at this line :
FirstName.getText().toString().trim().equals("")


The error is following:
Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
.

Here is my fragment_add_patient xml file which contains all items.

<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"
tools:context="com.example.ionut.myapplication.PatientController.AddPatientFragment">

<!-- TODO: Update blank fragment layout -->

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:id="@+id/PatientFirstName"
android:hint="First Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="27dp"
android:id="@+id/PatientLastName"
android:hint="Last Name"
android:layout_below="@+id/PatientFirstName"
android:layout_alignLeft="@+id/PatientFirstName"
android:layout_alignStart="@+id/PatientFirstName" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:layout_below="@+id/PatientLastName"
android:layout_alignRight="@+id/PatientLastName"
android:layout_alignEnd="@+id/PatientLastName"
android:layout_marginTop="25dp"
android:id="@+id/Email"
android:hint="Email" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_marginTop="22dp"
android:id="@+id/PatientAge"
android:hint="Age"
android:layout_below="@+id/Email"
android:layout_alignLeft="@+id/Email"
android:layout_alignStart="@+id/Email" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:layout_below="@+id/PatientAge"
android:layout_alignLeft="@+id/PatientAge"
android:layout_alignStart="@+id/PatientAge"
android:layout_marginTop="32dp"
android:id="@+id/PatientPhone"
android:hint="Phone" />


<Button
android:text="Add Patient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AddPatientButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="77dp" />

<Spinner
android:id="@+id/cspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_below="@+id/PatientPhone"
android:layout_alignRight="@+id/PatientPhone"
android:layout_alignEnd="@+id/PatientPhone"
android:layout_alignLeft="@+id/PatientPhone"
android:layout_alignStart="@+id/PatientPhone" />




Thanks in advance!.

Answer

The FirstName EditText is null because the findViewById method is trying to find an editext with the id of FirstName but the correct id is PatientFirstName

To solve the issue, replace the

final EditText FirstName=(EditText) v.findViewById(R.id.FirstName);

with

final EditText FirstName=(EditText) v.findViewById(R.id.PatientFirstName);