我目前正在尝试创建注册页面的项目。下面是我的两个活动的代码:
我的开始活动代码如下:
package com.example.profmox.k_mart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Start extends AppCompatActivity {
private Button regButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
regButton = (Button)findViewById(R.id.register);
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent reg_intent = new Intent(Start.this,Register.class);
startActivity(reg_intent);
}
});
}
}
和我的Register类代码:
package com.example.profmox.k_mart;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Register extends AppCompatActivity {
private TextInputLayout regName,regEmail,regPassword;
private Button regCreate;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
regName = (TextInputLayout)findViewById(R.id.reg_displayName);
regEmail = (TextInputLayout)findViewById(R.id.reg_email);
regPassword = (TextInputLayout)findViewById(R.id.reg_password);
regCreate = (Button)findViewById(R.id.reg_create);
regCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String display_name = regName.getEditText().getText().toString();
String email = regEmail.getEditText().getText().toString();
String password = regPassword.getEditText().getText().toString();
registerUser(display_name,email,password);
}
});
}
private void registerUser(String display_name, String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
startActivity(new Intent(Register.this, MainActivity.class));
finish();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(Register.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
我的开始活动的布局代码:
<?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"
tools:context=".Start">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Welcome to K-Mart"
android:textSize="24dp"/>
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="126dp"
android:text="@string/need_a_new_account" />
</RelativeLayout>
我的注册活动的布局代码:
<?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">
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="90dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/reg_displayName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/display_name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/reg_email"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/reg_password"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/reg_email">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/reg_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="@string/create_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/reg_password" />
</android.support.constraint.ConstraintLayout>
但是每次我在启动活动中单击按钮时,它都会使应用程序崩溃并发出以下错误消息:
08-21 15:55:34.884 9689-9689/com.example.profmox.k_mart E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.profmox.k_mart, PID: 9689
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.profmox.k_mart/com.example.profmox.k_mart.Register}: java.lang.ClassCastException: android.support.design.widget.TextInputLayout cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassCastException: android.support.design.widget.TextInputLayout cannot be cast to android.widget.EditText
at com.example.profmox.k_mart.Register.onCreate(Register.java:31)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我如何解决此问题,因为我无法在代码中的任何地方显示错误。
答案 0 :(得分:0)
密码android.support.design.widget.TextInputLayout instead of android.support.design.widget.EditTextLayout
同时投射视图
答案 1 :(得分:0)
更改注册活动的布局:
<?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=".MainActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout1"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="90dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/reg_displayName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="string/display_name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout2"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout1">
<android.support.design.widget.TextInputEditText
android:id="@+id/reg_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="string/email" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout3"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout2">
<android.support.design.widget.TextInputEditText
android:id="@+id/reg_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="string/password" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/reg_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="string/create_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout3" />
</android.support.constraint.ConstraintLayout>
更改:
private TextInputEditText regName,regEmail,regPassword;
regName = (TextInputEditText)findViewById(R.id.reg_displayName);
regEmail = (TextInputEditText)findViewById(R.id.reg_email);
regPassword = (TextInputEditText)findViewById(R.id.reg_password);
和
public void onClick(View view) {
String display_name = regName.getText().toString();
String email = regEmail.getText().toString();
String password = regPassword.getText().toString();
registerUser(display_name,email,password);
}
答案 2 :(得分:0)
将您的TextInputLayout更改为TextInputEditText
private TextInputEditText regName,regEmail,regPassword;
regName = (TextInputEditText)findViewById(R.id.reg_displayName);
regEmail = (TextInputEditText)findViewById(R.id.reg_email);
regPassword = (TextInputEditText)findViewById(R.id.reg_password);
还
regCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String display_name = regName.getText().toString();
String email = regEmail.getText().toString();
String password = regPassword.getText().toString();
registerUser(display_name,email,password);
}
});