使用onClick功能获取编辑文本字段时遇到问题



我正在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(( 方法中初始化。

您的布局显示

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

相关内容

最新更新