简体   繁体   中英

Having trouble getting EditText field with onClick function

I am building a client application on android, and I have a problem getting my input in EditText objects. I am having the error message : android.text.Editable android.widget.EditText.getText()' on a null object reference

    package com.example.tom.friendlyhousingandroid;

import android.os.Debug;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

public class register extends AppCompatActivity {
    public EditText passwordText;
    public EditText emailText;
    public EditText firstnameText;
    public EditText lastnameText;
    public String password,email,lastname,firstname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        passwordText   = findViewById(R.id.password);
        emailText   = findViewById(R.id.email);
        firstnameText   = findViewById(R.id.firstnameregister);
        lastnameText   = findViewById(R.id.lastnameregister);
    }

    public void Registration(View view){

        password = passwordText.getText().toString();
        email = emailText.getText().toString();
        firstname = firstnameText.getText().toString();
        lastname = lastnameText.getText().toString();
        RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
        String url = "http://172.16.10.92:8080/school/rest/users/addUser";
        StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("response",response);
            }
        }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
            @Override
            public void onErrorResponse(VolleyError error) {
                //This code is executed if there is an error.
            }
        }) {
            protected Map<String, String> getParams() {

                Map<String, String> MyData = new HashMap<String, String>();
                MyData.put("password", "fdgfr"); //Add the data you'd like to send to the server.
                MyData.put("email", "fdgfr");
                MyData.put("first_name", "fdgfr");
                MyData.put("last_name", "fdgfr");
                /*Log.d("password",password);
                Log.d("firstname",firstname);
                Log.d("lastname",lastname);
                Log.d("email",email);*/
                return MyData;
            }
        };


        MyRequestQueue.add(MyStringRequest);

    }
}

and the XML FILE :

    <?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=".register">

    <EditText
        android:id="@+id/emailregister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="44dp"
        android:ems="10"
        android:hint="email address"
        android:inputType="textEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/lastnameregister" />

    <EditText
        android:id="@+id/passwordregister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp"
        android:ems="10"
        android:hint="password"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/emailregister" />

    <EditText
        android:id="@+id/lastnameregister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="43dp"
        android:ems="10"
        android:hint="last name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/firstnameregister" />

    <EditText
        android:id="@+id/firstnameregister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="first name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/launchregistration"
        android:layout_width="206dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="95dp"
        android:layout_marginTop="96dp"
        android:text="Register"
        android:onClick="Registration"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordregister" />
</android.support.constraint.ConstraintLayout>

Obviously I am missing something here. I think the initialisation is not done well and the objects are not initialised in the onCreate() method.

Your layout says

android:id="@+id/emailregister"

but your code says:

emailText   = findViewById(R.id.email);

I'm betting you want to be using R.id.emailregister instead.

Id used for email and password into java code and in XML are different. Those must be same. So rewrite your java code for finding views as

passwordText   = findViewById(R.id.passwordregister);
emailText   = findViewById(R.id.emailregister);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM