我正在为我的应用进行Firebase电话身份验证,因此用户可以注册,也可以确认他的电话号码。 我将通过Firebase电话身份验证来实现这一点。
首先,用户在EditText
中键入其电话,姓名,密码和用于密码恢复的安全代码,然后用户点击“继续”按钮,这将使他在另一活动中验证其电话号码(我将管理Firebase电话验证)。
但是问题是我需要在第一个活动中使用在所有4 EditText
中键入的文本用户才能上载到Firebase。
它无法识别第一个活动中的edittext,因此无法将任何内容更新为firebase。
并在尝试进行验证活动时抛出此错误
E/UncaughtException: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cyber.eatit/com.cyber.eatit.Verify}: java.lang.InstantiationException: class com.cyber.eatit.Verify has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556)
at android.app.ActivityThread.access$800(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5568)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Caused by: java.lang.InstantiationException: class com.cyber.eatit.Verify has no zero argument constructor
at java.lang.Class.newInstance(Class.java:1563)
at android.app.Instrumentation.newActivity(Instrumentation.java:1070)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2385)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556)
at android.app.ActivityThread.access$800(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5568)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Caused by: java.lang.NoSuchMethodException: <init> []
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getDeclaredConstructor(Class.java:510)
at java.lang.Class.newInstance(Class.java:1561)
at android.app.Instrumentation.newActivity(Instrumentation.java:1070)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2385)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2556)
at android.app.ActivityThread.access$800(ActivityThread.java:170)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5568)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
这是第一个活动的代码段,用于处理EditText
和按钮事件:
edtcontinue.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View view) {
if (Common.isConnectedToInternet(getActivity())) {
table_user.addValueEventListener(new ValueEventListener() {@
Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Check if entered phone number
ph = edtPhone.getText().toString();
if (ph.matches("")) {
YoYo.with(Techniques.Shake)
.duration(700)
.repeat(1)
.playOn(edtPhone);
MDToast mdqeToaswet = MDToast.makeText(getActivity(), "You didn't entered a phone number", Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
mdqeToaswet.show();
return;
}
//Check if entered a name
//Check if entered phone number
nam = edtName.getText().toString();
if (nam.matches("")) {
YoYo.with(Techniques.Shake)
.duration(700)
.repeat(1)
.playOn(edtName);
MDToast mdqeToaswpet = MDToast.makeText(getActivity(), "You didn't entered your name", Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
mdqeToaswpet.show();
return;
}
//Check if entered password
pass = edtPassword.getText().toString();
if (pass.matches("")) {
YoYo.with(Techniques.Shake)
.duration(700)
.repeat(1)
.playOn(edtPassword);
MDToast mdqToaswet = MDToast.makeText(getActivity(), "You didn't entered a password", Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
mdqToaswet.show();
return;
}
if (TextUtils.isEmpty(pass) || pass.length() < 8) {
YoYo.with(Techniques.Shake)
.duration(700)
.repeat(1)
.playOn(edtPassword);
edtPassword.setError("You must have 8 characters in your password");
return;
}
//Check if entered a secure code
secu = edtSecureCode.getText().toString();
if (secu.matches("")) {
YoYo.with(Techniques.Shake)
.duration(700)
.repeat(1)
.playOn(edtSecureCode);
MDToast mdqToaswuet = MDToast.makeText(getActivity(), "You didn't entered your secure code", Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
mdqToaswuet.show();
return;
}
if (TextUtils.isEmpty(secu) || secu.length() < 4) {
YoYo.with(Techniques.Shake)
.duration(700)
.repeat(1)
.playOn(edtSecureCode);
edtSecureCode.setError("You must have at least 4 characters in your secure code");
return;
}
try {
//Check if already user phone exist
if (dataSnapshot.child(edtPhone.getText().toString()).exists()) {
MDToast mdqToaset = MDToast.makeText(getActivity(), "Phone number already registered ", Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
mdqToaset.show();
} else {
String code = CountryData.countryAreaCodes[spinner.getSelectedItemPosition()];
String number = edtPhone.getText().toString().trim();
String phoneNumber = "+" + code + number;
Intent verify = new Intent(getActivity(), Verify.class);
verify.putExtra("phonenumber", phoneNumber);
verify.putExtra("phone", edtPhone.getText().toString());
startActivity(verify);
}
} catch (Exception e) {}
}@
Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});
} else {
MDToast mdqToasetuo = MDToast.makeText(getActivity(), "Please check your internet connection", Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
mdqToasetuo.show();
return;
}
}
});
这是第二个活动,在这里我需要使用上一个活动中的edittext,因此它可以上传到Firebase(如果已确认电话号码):
private void signInWithCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener < AuthResult > () {@
Override
public void onComplete(@NonNull Task < AuthResult > task) {
if (task.isSuccessful()) {
//here, this code for upload
try {
User user = new User(edtName.getText().toString(),
edtPassword.getText().toString(),
edtSecureCode.getText().toString()
);
table_user.child(edtPhone.getText().toString()).setValue(user);
MDToast mdqToasetu = MDToast.makeText(Verify.this, "Sign up successfully :) ", Toast.LENGTH_SHORT, MDToast.TYPE_SUCCESS);
mdqToasetu.show();
} catch (Exception e) {}
} else {
MDToast ds = MDToast.makeText(Verify.this, task.getException().getMessage(), Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
ds.show();
}
}
});
}