使用onClick函数获取EditText字段时遇到问题

时间:2018-09-19 19:03:35

标签: java android

我正在android上构建客户端应用程序,并且在EditText对象中获取输入时遇到问题。我收到错误消息:空对象引用上的android.text.Editable android.widget.EditText.getText()'

    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);

    }
}

和XML文件:

    <?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>

很明显,我在这里错过了一些东西。我认为初始化工作做得不好,对象也没有在onCreate()方法中初始化。

2 个答案:

答案 0 :(得分:2)

您的布局显示

android:id="@+id/emailregister"

但是您的代码显示:

emailText   = findViewById(R.id.email);

我敢打赌,您想改用R.id.emailregister

答案 1 :(得分:1)

用于电子邮件和Java代码以及XML密码的ID是不同的。这些必须相同。因此,将您的Java代码重写为

来查找视图
passwordText   = findViewById(R.id.passwordregister);
emailText   = findViewById(R.id.emailregister);