英文:
Firebase Custom Userfield crash - Android Studio
问题
以下是已翻译好的代码部分:
public class SignUp extends AppCompatActivity {
//变量
private EditText regName, regNpm, regUsername, regEmail, regPassword, regCoPassword;
private Button regBtn, regToLoginBtn;
ProgressBar signUp_progress;
private DatabaseReference databaseReference;
private FirebaseDatabase firebaseDatabase;
private FirebaseAuth mAuth;
String fullname, username, email, npm, password, co_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_sign_up);
signUp_progress = findViewById(R.id.signUp_progress);
regName = findViewById(R.id.reg_name);
regNpm = findViewById(R.id.reg_npm);
regUsername = findViewById(R.id.reg_username);
regEmail = findViewById(R.id.reg_email);
regPassword = findViewById(R.id.reg_password);
regCoPassword = findViewById(R.id.reg_CoPassword);
regBtn = findViewById(R.id.reg_btn);
regToLoginBtn = findViewById(R.id.reg_login_btn);
mAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("UserData");
regToLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
}
});
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!validateFullname() | !validateUsername() | !validateEmail() | !validatePassword() | !validateNpm()) {
return;
}
if (regPassword.equals(regCoPassword)) {
signUp_progress.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, String.valueOf(regPassword)).addOnCompleteListener
(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
UserData data = new UserData(fullname, username, email, npm);
FirebaseDatabase.getInstance().getReference("UserData")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(data).
addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
signUp_progress.setVisibility(View.GONE);
Toast.makeText(SignUp.this, "Successful Registered", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SignUp.this, MenuActivity.class);
startActivity(intent);
finish();
}
});
} else {
signUp_progress.setVisibility(View.GONE);
Toast.makeText(SignUp.this, "Check Email id or Password", Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(SignUp.this, "Password didn't match", Toast.LENGTH_SHORT).show();
}
}
});
}
// 省略其他部分...
}
public class Login extends AppCompatActivity {
Button callSignUp, loginBtn;
ImageView image;
TextView logoText, sloganText;
TextInputLayout username, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
callSignUp = findViewById(R.id.signup_screen);
image = findViewById(R.id.logo_image);
logoText = findViewById(R.id.logo_text);
sloganText = findViewById(R.id.slogan_text);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
loginBtn = findViewById(R.id.login_btn);
callSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Login.this,SignUp.class);
Pair[] pairs = new Pair[7];
pairs[0] = new Pair<View,String>(image,"logo_image");
pairs[1] = new Pair<View,String>(logoText,"logo_text");
pairs[2] = new Pair<View,String>(sloganText,"logo_desc");
pairs[3] = new Pair<View,String>(username,"uname_tran");
pairs[4] = new Pair<View,String>(password,"password_tran");
pairs[5] = new Pair<View,String>(loginBtn,"button_tran");
pairs[6] = new Pair<View,String>(callSignUp,"login_signup_tran");
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(Login.this,pairs);
startActivity(intent, options.toBundle());
}
});
}
}
以上是代码的翻译部分,包括SignUp.java
和Login.java
中的关键部分。至于出现的错误日志,是因为在SignUp.java
中的布局文件中使用了com.google.android.material.textfield.TextInputLayout
,但在代码中使用findViewById
查找对应的组件时,使用了EditText
而不是TextInputLayout
。需要修改相关代码以正确处理布局中的组件。
英文:
As the title said. I want to make a custom authentication with username on Register form. All code was good and there is no error. But when I'm trying to run the app, It's force close when After I click the Registration button from the Login page. So this is the list code in SignUp.java
public class SignUp extends AppCompatActivity {
//Variabel
private EditText regName, regNpm, regUsername, regEmail, regPassword, regCoPassword;
private Button regBtn, regToLoginBtn;
ProgressBar signUp_progress;
private DatabaseReference databaseReference;
private FirebaseDatabase firebaseDatabase;
private FirebaseAuth mAuth;
String fullname, username, email, npm, password, co_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Line dibawah untuk menghilangkan status bar dari screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_sign_up);
//Hooks ke semua elemen xml di activity_sign_up.xml
signUp_progress = findViewById(R.id.signUp_progress);
regName = findViewById(R.id.reg_name);
regNpm = findViewById(R.id.reg_npm);
regUsername = findViewById(R.id.reg_username);
regEmail = findViewById(R.id.reg_email);
regPassword = findViewById(R.id.reg_password);
regCoPassword = findViewById(R.id.reg_CoPassword);
regBtn = findViewById(R.id.reg_btn);
regToLoginBtn = findViewById(R.id.reg_login_btn);
// Get Firebase auth instance
mAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("UserData");
regToLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
}
});
// handle user SignUp button
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!validateFullname() | !validateUsername() | !validateEmail() | !validatePassword() | !validateNpm()) {
return;
}
if (regPassword.equals(regCoPassword)) {
// progressbar VISIBLE
signUp_progress.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, String.valueOf(regPassword)).addOnCompleteListener
(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
UserData data = new UserData(fullname, username, email, npm);
FirebaseDatabase.getInstance().getReference("UserData")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(data).
addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// progressbar GONE
signUp_progress.setVisibility(View.GONE);
Toast.makeText(SignUp.this, "Successful Registered", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SignUp.this, MenuActivity.class);
startActivity(intent);
finish();
}
});
} else {
// progressbar GONE
signUp_progress.setVisibility(View.GONE);
Toast.makeText(SignUp.this, "Check Email id or Password", Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(SignUp.this, "Password didn't match", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean validateFullname() {
fullname = regName.getText().toString().trim();
if (TextUtils.isEmpty(fullname)) {
Toast.makeText(SignUp.this, "Enter Your Full Name", Toast.LENGTH_SHORT).show();
return false;
} else {
return true;
}
}
private boolean validateUsername() {
username = regUsername.getText().toString().trim();
if (TextUtils.isEmpty(username)) {
Toast.makeText(SignUp.this, "Enter Your User Name", Toast.LENGTH_SHORT).show();
return false;
} else {
return true;
}
}
private boolean validateNpm() {
npm = regNpm.getText().toString().trim();
if (TextUtils.isEmpty(npm)) {
Toast.makeText(SignUp.this, "Enter Your NPM", Toast.LENGTH_SHORT).show();
return false;
} else {
return true;
}
}
private boolean validateEmail() {
email = regEmail.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(SignUp.this, "Enter Your Email", Toast.LENGTH_SHORT).show();
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
Toast.makeText(SignUp.this, "Please enter valid Email", Toast.LENGTH_SHORT).show();
return false;
} else {
return true;
}
}
private boolean validatePassword() {
password = regPassword.getText().toString().trim();
co_password = regCoPassword.getText().toString().toLowerCase();
if (TextUtils.isEmpty(password)) {
Toast.makeText(SignUp.this, "Enter Your Password", Toast.LENGTH_SHORT).show();
return false;
} else if (TextUtils.isEmpty(co_password)) {
Toast.makeText(SignUp.this, "Enter Your Co-Password", Toast.LENGTH_SHORT).show();
return false;
} else if (password.length() <= 6) {
Toast.makeText(SignUp.this, "Password is Very Short", Toast.LENGTH_SHORT).show();
return false;
} else {
return true;
}
}
// if the user already logged in then it will automatically send on Dashboard/MainActivity activity.
@Override
public void onStart() {
super.onStart();
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
Intent intent = new Intent(SignUp.this, MenuActivity.class);
startActivity(intent);
}
}
}
And this is the listing code from Login.java
public class Login extends AppCompatActivity {
//Variabel
Button callSignUp, loginBtn;
ImageView image;
TextView logoText, sloganText;
TextInputLayout username, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Line dibawah untuk menghilangkan status bar dari screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
//Hooks
callSignUp = findViewById(R.id.signup_screen);
image = findViewById(R.id.logo_image);
logoText = findViewById(R.id.logo_text);
sloganText = findViewById(R.id.slogan_text);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
loginBtn = findViewById(R.id.login_btn);
callSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Login.this,SignUp.class);
Pair[] pairs = new Pair[7];
pairs [0] = new Pair<View,String>(image,"logo_image");
pairs [1] = new Pair<View,String>(logoText,"logo_text");
pairs [2] = new Pair<View,String>(sloganText,"logo_desc");
pairs [3] = new Pair<View,String>(username,"uname_tran");
pairs [4] = new Pair<View,String>(password,"password_tran");
pairs [5] = new Pair<View,String>(loginBtn,"button_tran");
pairs [6] = new Pair<View,String>(callSignUp,"login_signup_tran");
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(Login.this,pairs);
startActivity(intent, options.toBundle());
}
});
}
}
And then there's logcat
2020-10-13 00:24:08.213 5297-5336/com.hardy.gunadarmastudentapp D/EGL_emulation: eglMakeCurrent: 0x77c26384a380: ver 2 0 (tinfo 0x77c2f32f8280)
2020-10-13 00:24:08.225 5297-5336/com.hardy.gunadarmastudentapp D/EGL_emulation: eglMakeCurrent: 0x77c26384a380: ver 2 0 (tinfo 0x77c2f32f8280)
2020-10-13 00:24:08.230 5297-5336/com.hardy.gunadarmastudentapp E/BufferQueueProducer: [unnamed-5297-8] setMaxDequeuedBufferCount: 2 dequeued buffers would exceed the maxBufferCount (2) (maxAcquired 1 async 0 mDequeuedBufferCannotBlock 0)
2020-10-13 00:24:08.230 5297-5336/com.hardy.gunadarmastudentapp E/Surface: IGraphicBufferProducer::setBufferCount(3) returned Invalid argument
2020-10-13 00:24:08.231 5297-5336/com.hardy.gunadarmastudentapp D/EGL_emulation: eglMakeCurrent: 0x77c26384a380: ver 2 0 (tinfo 0x77c2f32f8280)
2020-10-13 00:24:08.240 5297-5336/com.hardy.gunadarmastudentapp D/EGL_emulation: eglMakeCurrent: 0x77c26384a380: ver 2 0 (tinfo 0x77c2f32f8280)
2020-10-13 00:24:08.244 5297-5336/com.hardy.gunadarmastudentapp D/EGL_emulation: eglMakeCurrent: 0x77c26384a380: ver 2 0 (tinfo 0x77c2f32f8280)
2020-10-13 00:24:08.251 5297-5297/com.hardy.gunadarmastudentapp W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@5690764
2020-10-13 00:24:08.257 5297-5333/com.hardy.gunadarmastudentapp V/FA: onActivityCreated
2020-10-13 00:24:08.259 5297-5339/com.hardy.gunadarmastudentapp V/FA: Recording user engagement, ms: 12594
2020-10-13 00:24:08.261 5297-5339/com.hardy.gunadarmastudentapp V/FA: Connecting to remote service
2020-10-13 00:24:08.267 5297-5339/com.hardy.gunadarmastudentapp V/FA: Activity paused, time: 130626
2020-10-13 00:24:08.288 5297-5297/com.hardy.gunadarmastudentapp I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
2020-10-13 00:24:08.312 5297-5297/com.hardy.gunadarmastudentapp I/chatty: uid=10134(com.hardy.gunadarmastudentapp) identical 4 lines
2020-10-13 00:24:08.320 5297-5297/com.hardy.gunadarmastudentapp I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
2020-10-13 00:24:08.332 5297-5339/com.hardy.gunadarmastudentapp V/FA: Connection attempt already in progress
2020-10-13 00:24:08.576 5297-5297/com.hardy.gunadarmastudentapp D/AndroidRuntime: Shutting down VM
2020-10-13 00:24:09.200 5297-5297/com.hardy.gunadarmastudentapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hardy.gunadarmastudentapp, PID: 5297
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hardy.gunadarmastudentapp/com.hardy.gunadarmastudentapp.SignUp}: java.lang.ClassCastException: com.google.android.material.textfield.TextInputLayout cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.ClassCastException: com.google.android.material.textfield.TextInputLayout cannot be cast to android.widget.EditText
at com.hardy.gunadarmastudentapp.SignUp.onCreate(SignUp.java:47)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
2020-10-13 00:24:10.531 5297-5297/com.hardy.gunadarmastudentapp I/Process: Sending signal. PID: 5297 SIG: 9
Then the activity_sign_up.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".SignUp"
android:orientation="vertical"
android:background="#8f4dc9"
android:padding="20dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:transitionName="logo_image"
android:src="@drawable/logogundar"/>
<TextView
android:id="@+id/logo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/acme"
android:text="Menu Register"
android:textSize="30sp"
android:transitionName="logo_text"
android:textColor="#FFF"/>
<TextView
android:id="@+id/slogan_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Isi NPM, username, dan email untuk sign in"
android:textColor="#FFF"
android:textSize="15sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_name"
android:hint="Nama"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_npm"
android:hint="NPM"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_username"
android:hint="Username"
app:counterEnabled="true"
app:counterMaxLength="15"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_email"
android:hint="Email"
app:passwordToggleEnabled="true"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_password"
android:hint="Password"
app:passwordToggleEnabled="true"
android:transitionName="password_tran"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/reg_CoPassword"
android:hint="Confirm Password"
app:passwordToggleEnabled="true"
android:transitionName="coPassword_tran"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/reg_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:background="#000"
android:textColor="#FFF"/>
<Button
android:id="@+id/reg_login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Udah ada akun? Sign in sini"
android:background="#00000000"
android:textColor="#FFF"/>
</LinearLayout>
<ProgressBar
android:id="@+id/signUp_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
答案1
得分: 1
它报告 ClassCastException,因为您将 TextInputLayout 误当作 EditText 处理。为包装的 editText 设置单独的 id,并在该视图上调用 getText(),而不是调用 TextInputLayout 上的方法。
英文:
it says ClassCastException because you treat TextInputLayout as EditText. Set a separate id for for the wrapped editText and call getText() on that view instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论