我对android编程很新,我无法弄清楚这里有什么问题。 我有一个注册表格,在发送注册信息和去另一个活动之前验证。表单验证是否有效,如果为false,但是当输入所有字段时,单击按钮时出现错误。
Java代码:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class registracijaLastnika extends AppCompatActivity {
EditText etName;
EditText etSurname;
EditText etPasswordL;
EditText etUsernameL;
EditText etEmailL;
EditText etRepeatPasswordL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registracija_lastnika);
etName = (EditText) findViewById(R.id.etIme);
etSurname = (EditText) findViewById(R.id.etPriimek);
etPasswordL = (EditText) findViewById(R.id.etPasswordL);
etUsernameL = (EditText) findViewById(R.id.etUsernameL);
etEmailL = (EditText) findViewById(R.id.etEmailL);
etRepeatPasswordL = (EditText) findViewById(R.id.etRepeatPasswordL);
final Button bRegisterL = (Button) findViewById(R.id.bRegisterL);
bRegisterL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String type = "regLas";
String ime = etName.getText().toString();
String priimek = etSurname.getText().toString();
String uporabnisko_ime = etUsernameL.getText().toString();
String geslo = etPasswordL.getText().toString();
String email = etEmailL.getText().toString();
validate();
if(!validate()) {
Toast.makeText(getApplicationContext(), "Signup has Failed",Toast.LENGTH_SHORT).show();
}
else {
BackgroundWorker backgroundWorker = new BackgroundWorker(getApplicationContext());
backgroundWorker.execute(type, ime, priimek, uporabnisko_ime, geslo, email);
Intent registerIntent = new Intent(registracijaLastnika.this, registracijaKavarne.class);
registracijaLastnika.this.startActivity(registerIntent);
}
}
});}
public boolean validate(){
boolean valid = true;
if(etName.length()<1 || etName.length()>32){
etName.setError("Please enter valid name");
valid = false;
}
if(etSurname.length()<1 || etSurname.length()>32){
etSurname.setError("Please enter valid name");
valid = false;
}
if(etEmailL.length()<1 || !Patterns.EMAIL_ADDRESS.matcher((CharSequence) etEmailL).matches()){
etEmailL.setError("Please enter valid email address");
valid = false;
}
if(etPasswordL.length()<1){
etPasswordL.setError("Please enter valid password");
valid = false;
}
if(etRepeatPasswordL.length()<1){
etRepeatPasswordL.setError("Please enter valid password");
valid = false;
}
if(!etRepeatPasswordL.equals(etPasswordL)){
etPasswordL.setError("Passwords don't match!");
valid = false;
}
if(etUsernameL.length()<1 || etUsernameL.length()>32){
etUsernameL.setError("Please enter valid name");
valid = false;
}
return valid;
}
}
此活动的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_registracija_lastnika"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lipic.coffeeaddict.registracijaLastnika">
<TextView
android:text="@string/personal_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tNaslov2L"
android:layout_marginTop="21dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:layout_below="@+id/tNaslovL"
android:layout_alignParentStart="true"
android:textStyle="normal|bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etUsernameL"
android:hint="@string/username"
android:layout_marginTop="11dp"
android:layout_below="@+id/etPriimek"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/etEmailL"
android:hint="@string/email"
android:layout_marginTop="13dp"
android:layout_below="@+id/etRepeatPasswordL"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etPriimek"
android:hint="@string/Surname"
android:layout_marginTop="7dp"
android:layout_below="@+id/etIme"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/etRepeatPasswordL"
android:hint="@string/Rpassword"
android:layout_marginTop="10dp"
android:layout_below="@+id/etPasswordL"
android:layout_alignParentStart="true" />
<Button
android:text="@string/proceed_to_caffeteria_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bRegisterL"
android:onClick="regLas"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="70dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etIme"
android:layout_marginTop="12dp"
android:hint="@string/Name"
android:layout_below="@+id/tNaslov2L"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/etPasswordL"
android:hint="@string/password"
android:layout_marginTop="13dp"
android:inputType="textPassword"
android:layout_below="@+id/etUsernameL"
android:layout_alignParentStart="true" />
<TextView
android:text="@string/owner_registration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tNaslovL"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
BackgroundWorker代码:
private String backgroundRegLas(String regLasURL, String... params){
try {
String ime = params[1];
String priimek = params[2];
String uporabnisko_ime = params[3];
String geslo = params[4];
String email = params[5];
URL url = new URL(regLasURL);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("ime","UTF-8")+"="+ URLEncoder.encode(ime,"UTF-8")+"&"
+URLEncoder.encode("priimek","UTF-8")+"="+ URLEncoder.encode(priimek,"UTF-8")+"&"
+URLEncoder.encode("uporabnisko_ime","UTF-8")+"="+ URLEncoder.encode(uporabnisko_ime,"UTF-8")+"&"
+URLEncoder.encode("geslo","UTF-8")+"="+ URLEncoder.encode(geslo,"UTF-8")+"&"
+URLEncoder.encode("email","UTF-8")+"="+ URLEncoder.encode(email,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result= bufferedReader.readLine();
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这是错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lipic.coffeeaddict, PID: 3234
java.lang.ClassCastException: android.support.v7.widget.AppCompatEditText cannot be cast to java.lang.CharSequence
at com.example.lipic.coffeeaddict.registracijaLastnika.validate(registracijaLastnika.java:76)
at com.example.lipic.coffeeaddict.registracijaLastnika$1.onClick(registracijaLastnika.java:53)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
申请已终止。
感谢您的帮助。
答案 0 :(得分:0)
我认为问题在于验证方法
if(etName.length()<1 || etName.length()>32){
etName.setError("Please enter valid name");
valid = false;
}
这里你应该做的
etName.getText().length()
在验证方法中查看所有其他if条件并进行更改。