我正在尝试在我的android studio项目应用程序中注册用户,但在发送或注册数据之前,我正在使用" validations.java"验证输入的类,然后进行注册。
以下是代码: register.java
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
/**
* Created by Tanishq on 15-11-2015.
*/
public class register extends AppCompatActivity {
public EditText editname;
public EditText editemail;
public EditText editmobile;
public EditText editpassword;
public EditText editrepassword;
String error_code="";
boolean flagmob = false;
boolean flagemail = false;
boolean flagpass = false;
private static final String REGISTER_URL = "http://127.0.0.1/class/register.php";
String check="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
editname = (EditText)findViewById(R.id.name);
editemail = (EditText)findViewById(R.id.email);
editmobile = (EditText)findViewById(R.id.mobile);
editpassword = (EditText)findViewById(R.id.password);
editrepassword = (EditText)findViewById(R.id.repassword);
}
public void onregister(View view)
{
validations vd = new validations();
int k = vd.validate();
if(k==1)
{
Toast.makeText(register.this, "Registration Successful", Toast.LENGTH_LONG).show();
}
else
{
int len1 = error_code.length();
for(int i =0; i<-len1; i++)
{
char c = error_code.charAt(i);
if(c=='m')
{
flagmob=true;
}
else if(c=='e')
{
flagemail=true;
}
else if(c=='p')
{
flagpass=true;
}
}
if(flagmob=true)
{
editmobile.setHint("10 DIGIT VALID NUMBER");
editmobile.setText("");
editmobile.setHintTextColor(Color.parseColor("#F44336"));
}
if(flagemail=true)
{
editemail.setHint("ENTER VALID EMAIL ID");
editemail.setText("");
editemail.setHintTextColor(Color.parseColor("#F44336"));
}
if(flagpass=true)
{
editpassword.setHint("Passwords Don't Match");
editpassword.setText("");
editpassword.setHintTextColor(Color.parseColor("#F44336"));
editrepassword.setHint("Passwords Don't Match");
editrepassword.setText("");
editrepassword.setHintTextColor(Color.parseColor("#F44336"));
}
check = "";
error_code="";
}
}
}
验证类代码如下:我使用继承来从寄存器类中获取参数。
import com.classifiedwale.android.register;
public class validations extends register {
public int validate() {
int validation = 1;
int evalid = 1;
int mvalid = 1;
int pvalid = 1;
check = editmobile.getText().toString();
if (check.length() > 10) {
mvalid = 0;
error_code = error_code + "m";
}
//Email Validation
check = editemail.getText().toString();
char c;
int length = check.length();
for (int i = 0; i < length; i++) {
c = check.charAt(i);
if (c == '@') {
} else {
error_code = error_code + "e";
evalid = 0;
}
}
check = editpassword.getText().toString();
String check2 = editrepassword.getText().toString();
if (check.equals(check2)) {
validation = 1;
} else {
error_code = error_code + "p";
pvalid = 0;
}
if (evalid != 0 && mvalid != 0 && pvalid != 0) {
validation = 1;
} else {
validation = 0;
}
return validation;
}
}
然而错误就是这样:
11-15 07:12:42.210 19637-19637/com.classifiedwale.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:278)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.classifiedwale.android.validations.validate(validations.java:17)
at com.classifiedwale.android.register.onregister(register.java:49)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
请让我知道错误,我认为这有点与扩展课程的错误继承有关。
答案 0 :(得分:0)
你可以这样:
验证类:
public class Validation {
public boolean isEmailValid(CharSequence email){
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
public void validateMobile(String mobile){
if (mobile != null){
if (mobile.length() > 10){
//do stuff with name
}
}
}
public void validatePassword(String password, String retypePassword){
if (password != null && retypePassword != null){
if (password.equals(retypePassword)){
//do stuff with password
}
}
}
}
注册课程:
public class Register extends AppCompatActivity {
public EditText editname;
public EditText editemail;
public EditText editmobile;
public EditText editpassword;
public EditText editrepassword;
String error_code="";
boolean flagmob = false;
boolean flagemail = false;
boolean flagpass = false;
private static final String REGISTER_URL = "http://127.0.0.1/class/register.php";
String check="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
editname = (EditText)findViewById(R.id.name);
editemail = (EditText)findViewById(R.id.email);
editmobile = (EditText)findViewById(R.id.mobile);
editpassword = (EditText)findViewById(R.id.password);
editrepassword = (EditText)findViewById(R.id.repassword);
}
public void onregister(View view) {
Validation validation = new Validation();
String name = editname.getText().toString();
String email = editemail.getText().toString();
String mobile = editmobile.getText().toString();
String password = editpassword.getText().toString();
String retypePassword = editrepassword.getText().toString();
String validatedMobile = validation.validateMobile(mobile);
boolean validatedEmail = validation.isEmailValid(email);
String validatedPassword = validation.validatePassword(password, retypePassword);
//do stuff with validated data
}
}
希望它有所帮助!!!