gpt4 book ai didi

java - Activity 未停止响应

转载 作者:行者123 更新时间:2023-12-02 02:58:44 25 4
gpt4 key购买 nike

我是应用程序开发新手(android studio)。我有两个类,一个是主类,另一个类(数据库)连接到 firebase 并检查用户是否存在并分别回复 true 或 false,并打印出一些语句。问题是即使任务成功,android studio 也不会等待响应并返回 false。我应该在 logcat 中获取的打印语句应该是“这里 1”、“这里 2”,然后是“这里 3”。但它给了我“这里 2”、“这里 3”,然后是“这里 1”。

我知道它必须与线程或类似的事情有关。

public class loginpage extends AppCompatActivity {
private Button Login;
private EditText user_email_txt;
private EditText user_password_txt;
private ProgressBar wait_bar;
private String[] user_name_password ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

find_all_views();

Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getuser_login_details();
disable_all_views();
Activate_wait_bar();
final Database login = new Database();
boolean decision = login.sign_in(user_name_password[0], user_name_password[1]);
System.out.println("here 3");
}
});

}

private void find_all_views() {
user_email_txt = findViewById(R.id.UserName);
user_password_txt = findViewById(R.id.Password);
Login = findViewById(R.id.submit_button);
wait_bar = findViewById(R.id.wait_bar);
}

private void getuser_login_details() {
user_name_password = new String[2];
user_name_password[0] = user_email_txt.getText().toString();
user_name_password[1] = user_password_txt.getText().toString();
}

private void disable_all_views() {
user_email_txt.setVisibility(View.INVISIBLE);
user_password_txt.setVisibility(View.INVISIBLE);
Login.setVisibility(View.INVISIBLE);
}

private void Activate_wait_bar() {
wait_bar.setVisibility(View.VISIBLE);
}
}
public class Database  {
private FirebaseAuth connect_databse;
private boolean authorisation;
public Database() {}

public boolean sign_in(String user_name, String Password) {
authorisation = false;
connect_databse = FirebaseAuth.getInstance();
connect_databse.signInWithEmailAndPassword(user_name, Password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = connect_databse.getCurrentUser();
authorisation = true;
System.out.println("here 1");
}
}
});
System.out.println("here 2");
return authorisation;
}
}

如果任务成功,它应该返回authorization = true,并按顺序打印语句“here 1”,“here 2”,然后“here 3”。

最佳答案

创建一个新文件:

interface OnCompleteListener {
void onComplete();
}

在数据库类中:

 public class Database {

private FirebaseAuth connect_databse;

private boolean authorisation;

public Database() {

}

public void sign_in(String user_name, String Password,final OnCompleteListener listener) {

authorisation = false;

connect_databse = FirebaseAuth.getInstance();

connect_databse.signInWithEmailAndPassword(user_name, Password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = connect_databse.getCurrentUser();
authorisation = true;
System.out.println("here 1");
listener.onComplete();
System.out.println("here 2");
}
}
});
}

Activity 中:

Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

getuser_login_details();

disable_all_views();

Activate_wait_bar();

final Database login = new Database();

boolean decision = login.sign_in(user_name_password[0], user_name_password[1], new OnCompleteListener{
@Override
public void onComplete(){
System.out.println("here 3");
}
});

}
});

关于java - Activity 未停止响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57063021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com