简体   繁体   English

在内部类中访问如何解析变量,并且需要将其声明为final

[英]How to resolve variable is accessed within inner class and needs to be declared final

I have a log in activity which calls a method 'Log in' from another Java class. 我有一个登录活动,该活动从另一个Java类调用方法“登录”。 In the Log in activity, when the log in button is pressed it calls the method 'Log in' from another class which should return true if all the log in details are correct which allows the user to proceed. 在“登录”活动中,当按下“登录”按钮时,它将从另一个类调用“登录”方法,如果所有登录详细信息正确无误,则该方法将返回true,从而允许用户继续操作。

I have already tried declaring a global variable as final however this variable cannot be manipulated. 我已经尝试过将全局变量声明为final,但是无法操作该变量。

Code: 码:

public boolean LogIn(String email, String password) {
    boolean success = false;
    firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                success = true; //  Error here
            } else {
                success = false; // Error here
            }
        }
    });
    return success;
}

signInWithEmailAndPassword is asynchronous and returns immediately with a Task object the tracks the ongoing work. signInWithEmailAndPassword是异步的,并立即与Task对象一起返回,以跟踪正在进行的工作。 This means that LogIn also returns immediately with the initial value of success . 这意味着LogIn也会立即返回success的初始值。 Some time later, your completion listener on the Task will get invoked, whenever the data is ready. 一段时间后,只要数据准备就绪,就会调用Task的完成侦听器。 That's where you should deal with the results of the sign in. 那是您应处理登录结果的地方。

You're trying to turn an asynchronous method into a synchronous method. 您正在尝试将异步方法转换为同步方法。 This isn't a good idea. 这不是一个好主意。 Use the listener to handle the results. 使用侦听器处理结果。 You can't pass a value out of that listener. 您不能将值传递给该侦听器。

The error is you cant change the state of a local variable in anonymous inner class in java if you are using local variable in side a anonymous inner class those local variable will be considered as final. 错误是您无法在Java的匿名内部类中更改局部变量的状态,如果您在匿名内部类中使用局部变量,则这些局部变量将被视为最终变量。 In your case boolean success is local variable in logIn method it is considered as final variable you cannot re assign it in the anonymous inner class. 在您的情况下,布尔值成功是logIn方法中的局部变量,它被视为最终变量,您无法在匿名内部类中对其进行重新分配。

In your case think it will helps 就您而言,这会有所帮助

public boolean LogIn(String email, String password) { StringBuffer buffer = new StringBuffer("") : firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { buffer. append("true") ; } else { buffer. append("false") ; } } }); public boolean LogIn(String email,String password){StringBuffer buffer = new StringBuffer(“”):firebaseAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener(){@Override public void onComplete(@NonNull Task task){if (task.isSuccessful()){buffer。append(“ true”);} else {buffer。append(“ false”);}}}); return buffer. 返回缓冲区。 toString(). 的toString()。 equals("true") ; equals(“ true”); } }

您可以在此处最终使用AtomicBoolean在内部类中使用,但是由于该函数是异步的,因此未正确设置LogIn返回值,并且必须更改处理登录请求的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “从内部类访问变量需要声明为final”错误 - "variable is accessed from within inner class needs to be declared final" error 从内部类访问变量“ ”,需要声明为 final - Variable ' ' is accessed from within inner class, needs to be declared final 在内部类中访问变量。 需要宣布为最终 - Variable is accessed within inner class. Needs to be declared final 从内部 class 访问的变量需要声明为 final - Variable accessed from within inner class needs to be declared final 在内部类中访问变量“名称”。 需要宣布为最终 - Variable “name” is accessed within inner class. Needs to be declared final 变量(dialogView)是从内部类中访问的,需要声明为final - Variable (dialogView) is accessed from within inner class, needs to be declared final 需要在OnTouchListener中用MediaPlayer声明内部类中访问的变量的最终声明 - Variable Is Accessed Within Inner Class Needs to be Declared Final with MediaPlayer In a OnTouchListener 从内部类访问变量“i”,需要声明为 final - Variable 'i' is accessed from within inner class, needs to be declared final 变量&#39;btnsave&#39;是从内部类中访问的,需要声明为final - Variable 'btnsave' is accessed from within inner class, needs to be declared final 在内部类中访问变量。 需要声明为final。但我不想声明为final - Variable is accessed within inner class. Needs to be declared final.But I don't want to declared Final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM