Firebase身份验证对象在片段中崩溃



我在android java中使用firebase身份验证创建用户,当我按下创建帐户按钮时,应用程序崩溃,代码和logcat错误出现在之后

代码

*package com.example.database_project_admin.fragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.database_project_admin.R;
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;
import com.google.firebase.auth.FirebaseUser;
import java.util.concurrent.Executor;
/**
* A simple {@link Fragment} subclass.
*/
public class add_samesman_fragment extends Fragment
{
public add_samesman_fragment()
{
// Required empty public constructor
}
private FirebaseAuth mAuth;
RelativeLayout rellay1,rally2,rellay2;
TextView bottomTextView;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
rellay1.setVisibility(View.VISIBLE);
rally2.setVisibility(View.VISIBLE);
rellay2.setVisibility(View.VISIBLE);
}
};
EditText usernameTf,passeordTf;
ProgressBar progressBar;
Handler progressBarh=new Handler();
Runnable runnable1=new Runnable()
{
@Override
public void run()
{
progressBar.setVisibility(View.GONE);
}
};
Button login;
EditText emailTf,passwordTf,confirmPasswordTf;
TextView message;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_add_samesman_fragment, container, false);
rellay1 = v.findViewById(R.id.add_salesman_rellay1);
rally2=v.findViewById(R.id.add_salesman_bottom_rally2);
rellay2=v.findViewById(R.id.add_salesman_rellay2);
handler.postDelayed(runnable, 2000); //2000 is the timeout for the splash
progressBar=v.findViewById(R.id.add_salesman_my_progress_bar);
progressBarh.postDelayed(runnable1,100);
login=v.findViewById(R.id.add_salesman_login_button);
emailTf=v.findViewById(R.id.add_salesman_username_tf);
passwordTf=v.findViewById(R.id.add_salesman_password_tf);
confirmPasswordTf=v.findViewById(R.id.add_salesman_confirm_password_tf);
message=v.findViewById(R.id.add_salesman_message_text_view);
mAuth = FirebaseAuth.getInstance();
login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String email=emailTf.getText().toString();
String password=passwordTf.getText().toString();
String confirmPassword=confirmPasswordTf.getText().toString();
if(password.equals(confirmPassword))
{
if(password.length()>=6)
{
signupFunc(email,password);
progressBar.setVisibility(View.VISIBLE);
}
else
{
message.setText("passsword length must be greater than or equal than 6!!!!");
}
}else
{
message.setText("password donot match!!!!");
}
}
});
return v;
}
public void signupFunc(String email,String password)
{
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener((Executor) 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
progressBarh.postDelayed(runnable1,100);
} else {
// If sign in fails, display a message to the user.
progressBarh.postDelayed(runnable1,100);
Toast.makeText(getActivity(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
}*

logcat错误如下

2020-02-26 16:36:18.005 21425-21425/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-02-26 16:39:14.057 21425-21425/com.example.database_project_admin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.database_project_admin, PID: 21425
java.lang.ClassCastException: com.example.database_project_admin.fragments.add_samesman_fragment cannot be cast to java.util.concurrent.Executor
at com.example.database_project_admin.fragments.add_samesman_fragment.signupFunc(add_samesman_fragment.java:125)
at com.example.database_project_admin.fragments.add_samesman_fragment$3.onClick(add_samesman_fragment.java:107)
at android.view.View.performClick(View.java:6897)
at android.widget.TextView.performClick(TextView.java:12693)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

上面写着java.lang.ClassCastException:com.example.database_project_admin.fragments.add_samesman_fragment无法转换为java.util.courrent.Executor我不明白为什么请帮助

选项1:

您需要为您的片段实现Executor接口,以便将其转换为当前正在执行的Executor:.addOnCompleteListener((Executor) this,

您的类声明应该是public class add_samesman_fragment extends Fragment implements Executor,然后您需要在Fragment中实现execute()方法,如下所示:

@Override
public void execute(Runnable command) {
// TODO: Add your callback code here
}

选项2:

创建一个新的Executor对象,并将其传递给addOnCompleteListener()(而不是传递this(。

Executor executor = new Executor() {
@Override
public void execute(Runnable command) {
// TODO: Add your callback code here
}
};

最新更新