我需要在Android Studio上使用Java制作一个登录表单。注册新用户时出现问题。
RegisterActivity.java
package com.example.asus.loginpage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class RegisterActivity extends AppCompatActivity {
DatabaseHelper helper = new DatabaseHelper(this);
private EditText name2, email2, uname2, pass12, pass22;
private String namestr2, emailstr2, unamestr2, pass1str2, pass2str2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
public void onRegisterClick (View v)
{
View mView = getLayoutInflater().inflate(R.layout.activity_register, null);
if (v.getId()==R.id.breg) {
System.out.println("pencet");
name2 = (EditText) mView.findViewById(R.id.etfullname);
email2 = (EditText) mView.findViewById(R.id.etremail);
uname2 = (EditText) mView.findViewById(R.id.etruname);
pass12 = (EditText) mView.findViewById(R.id.etrpassword);
pass22 = (EditText) mView.findViewById(R.id.etconfpass);
emailstr2 = email2.getText().toString();
unamestr2 = uname2.getText().toString();
pass1str2 = pass12.getText().toString();
pass2str2 = pass22.getText().toString();
if (namestr2.length() == 0) {
name2.setError("Nama kosong, mohon di isi");
} else if (emailstr2.length() == 0) {
email2.setError("Email kosong, mohon di isi");
} else if (unamestr2.length() == 0) {
uname2.setError("username kosong, mohon di isi");
} else if (!pass1str2.equals(pass2str2)) {
Toast.makeText(RegisterActivity.this, "Password Unmatch", Toast.LENGTH_SHORT).show();
} else {
Contact c = new Contact();
c.setName(namestr2);
c.setEmail(emailstr2);
c.setUname(unamestr2);
c.setPass(pass1str2);
helper.insertContact(c);
finish();
Toast.makeText(RegisterActivity.this,
"Username & password berhasil dibuat",
Toast.LENGTH_SHORT).show();
}
}
}
}
这是xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:background="@drawable/background"
tools:context=".RegisterActivity">
<TextView
android:id="@+id/tvreg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="REGISTER"
android:textSize="30dp" />
<EditText
android:id="@+id/etremail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10"
android:hint="Email"
android:inputType="textPersonName" />
<EditText
android:id="@+id/etruname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="199dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName" />
<EditText
android:id="@+id/etfullname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="138dp"
android:ems="10"
android:hint="Full Name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/etrpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="205dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="@+id/etconfpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="142dp"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<Button
android:id="@+id/breg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="65dp"
android:text="Register"
android:onClick="onRegisterClick"/>
</RelativeLayout>
这是logcat
10-07 22:30:20.840 25805-25805/com.example.asus.loginpage E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asus.loginpage, PID: 25805
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.asus.loginpage.RegisterActivity.onRegisterClick(RegisterActivity.java:31)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
问题是,如果我点击“注册”按钮,它会在以下位置显示错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.asus.loginpage.RegisterActivity.onRegisterClick(RegisterActivity.java:31)
第31行的代码为pass12 = (EditText) mView.findViewById(R.id.etrpassword);
,我认为该语法没有问题。谁能协助我解决这个问题?
答案 0 :(得分:0)
请注意,NullPointerException是一个非常常见的错误,所有开发人员都会犯此错误,即使是最有经验的错误。纠正它的一个好的建议是:A)如果可能,请调试代码,逐步进行直到发现问题。 B)检查异常并了解何时可能发生。
从您的问题中,我看到了:
Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.asus.loginpage.RegisterActivity.onRegisterClick(RegisterActivity.java:31)
好像您尝试在未设置的对象中调用getText。
name2 = (EditText) mView.findViewById(R.id.etfullname);
email2 = (EditText) mView.findViewById(R.id.etremail);
uname2 = (EditText) mView.findViewById(R.id.etruname);
pass12 = (EditText) mView.findViewById(R.id.etrpassword);
pass22 = (EditText) mView.findViewById(R.id.etconfpass);
我认为其中一些变量(name2,email2,uname2,pass12,pass22)为空(调用findViewById
返回空)。
答案 1 :(得分:0)
使用以下代码简化onRegisterClick
方法。您的activity_register
已设置为“活动”布局,因此您无需再次对其进行充气。
public void onRegisterClick (View v) {
System.out.println("pencet");
name2 = (EditText) findViewById(R.id.etfullname);
email2 = (EditText) findViewById(R.id.etremail);
uname2 = (EditText) findViewById(R.id.etruname);
pass12 = (EditText) findViewById(R.id.etrpassword);
pass22 = (EditText) findViewById(R.id.etconfpass);
emailstr2 = email2.getText().toString();
unamestr2 = uname2.getText().toString();
pass1str2 = pass12.getText().toString();
pass2str2 = pass22.getText().toString();
if (namestr2.length() == 0) {
name2.setError("Nama kosong, mohon di isi");
} else if (emailstr2.length() == 0) {
email2.setError("Email kosong, mohon di isi");
} else if (unamestr2.length() == 0) {
uname2.setError("username kosong, mohon di isi");
} else if (!pass1str2.equals(pass2str2)) {
Toast.makeText(RegisterActivity.this, "Password Unmatch", Toast.LENGTH_SHORT).show();
} else {
Contact c = new Contact();
c.setName(namestr2);
c.setEmail(emailstr2);
c.setUname(unamestr2);
c.setPass(pass1str2);
helper.insertContact(c);
finish();
Toast.makeText(RegisterActivity.this,
"Username & password berhasil dibuat",
Toast.LENGTH_SHORT).show();
}
}
答案 2 :(得分:0)
您正在获取 NullPointerException ,因为您正在此处检查Null值的长度
if (namestr2.length() == 0) {
name2.setError("Nama kosong, mohon di isi");
}
您忘记为namestr2分配值
将此行添加到您的代码中
namestr2 = name2 .getText().toString();