Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用onClick函数获取EditText字段时遇到问题_Java_Android - Fatal编程技术网

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

Java 使用onClick函数获取EditText字段时遇到问题,java,android,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上构建一个客户端应用程序,在获取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);

    }
}
package com.example.tom.friendlyhousingandroid;
导入android.os.Debug;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.EditText;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
导入java.util.HashMap;
导入java.util.Map;
公共类注册扩展了AppCompatActivity{
公共编辑文本密码文本;
公共编辑文本电子邮件文本;
公共编辑文本firstnameText;
公共编辑文本lastnameText;
公共字符串密码、电子邮件、姓氏、名;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u寄存器);
passwordText=findviewbyd(R.id.password);
emailText=findViewById(R.id.email);
firstnameText=findviewbyd(R.id.firstnameregister);
lastnameText=findViewById(R.id.lastnameregister);
}
公共作废登记(查看){
password=passwordText.getText().toString();
email=emailText.getText().toString();
firstname=firstnameText.getText().toString();
lastname=lastnameText.getText().toString();
RequestQueue MyRequestQueue=Volley.newRequestQueue(this);
字符串url=”http://172.16.10.92:8080/school/rest/users/addUser";
StringRequest MyStringRequest=newStringRequest(Request.Method.POST,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.d(“响应”,响应);
}
},new Response.ErrorListener(){//创建一个错误侦听器以适当地处理错误。
@凌驾
公共无效onErrorResponse(截击错误){
//如果出现错误,将执行此代码。
}
}) {
受保护的映射getParams(){
Map MyData=newhashmap();
MyData.put(“password”、“fdgfr”);//添加要发送到服务器的数据。
MyData.put(“电子邮件”、“fdgfr”);
MyData.put(“名字”、“fdgfr”);
MyData.put(“姓氏”、“fdgfr”);
/*Log.d(“密码”,密码);
Log.d(“名字”,名字);
Log.d(“lastname”,lastname);
Log.d(“电子邮件”,电子邮件)*/
返回MyData;
}
};
添加(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()方法中没有初始化对象。

您的布局显示

android:id="@+id/emailregister"
但是你的代码说:

emailText   = findViewById(R.id.email);
我打赌你想用
R.id.emailregister
代替。

你的布局显示

android:id="@+id/emailregister"
但是你的代码说:

emailText   = findViewById(R.id.email);

我打赌你想改用
R.id.emailregister

用于电子邮件的id和在java代码和XML中的密码是不同的。它们必须是相同的。因此,请将查找视图的java代码重写为

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

在java代码和XML中用于电子邮件和密码的Id是不同的。它们必须是相同的。因此,请将查找视图的java代码重写为

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