java 找不到局部变量“ac”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40123024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:56:29  来源:igfitidea点击:

Cannot find local variable 'ac'

javaandroidandroid-debug

提问by S.BM

Below my code but it is not working - refer to this screenshot of the error during debugging,

在我的代码下方,但它不起作用 - 请参阅调试期间错误的此屏幕截图

Cannot find local variable 'ac'

找不到局部变量“ac”

    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccounts();
    for (final Account ac : accounts)
    {
        String acname = ac.name;
        System.out.println("Accounts : " + acname);

    }

回答by Iulian Popescu

From your screenshot I saw that you are using Android Studioand there is no problem with your code, but rather some debug settings. If you'll open Android Studiopreferences and go to Build, Execution, Deployment > Debugger > Data Viewsyou'll probably see the option Enable auto expressions in Variables viewticked, since it is ticked by default. It should look something like this: Android Studio PreferencesNow, if you'll check the IntelliJdocsfor that you'll find this (note that IntelliJand Android Studioare running on the same engine in case that you are wondering why I said about IntelliJdocs):

从您的屏幕截图中,我看到您正在使用Android Studio并且您的代码没有问题,但有一些调试设置。如果您打开Android Studio首选项并转到,Build, Execution, Deployment > Debugger > Data Views您可能会看到该选项被Enable auto expressions in Variables view勾选,因为它默认被勾选。它应该看起来像这样:Android Studio Preferences现在,如果你检查IntelliJ文档,你会发现这个(注意,IntelliJ并且Android Studio在同一引擎上运行,以防你想知道我为什么说IntelliJ文档):

Select this option if you want the IntelliJ IDEA debugger to automatically evaluate expressions and show the corresponding values in the Variables pane of the Debug tool window.

The debugger analyzes the context near the breakpoint (the current statement, one statement before, and one after). It does so to find various expressions in the source code (if available) such as, for example, myvar.myfield.

If such expressions don't contain explicit method invocations, the debugger evaluates them and shows the corresponding values in the Variables view.

如果您希望 IntelliJ IDEA 调试器自动计算表达式并在“调试”工具窗口的“变量”窗格中显示相应的值,请选择此选项。

调试器分析断点附近的上下文(当前语句,前一个语句,后一个语句)。它这样做是为了在源代码(如果可用)中查找各种表达式,例如 myvar.myfield。

如果此类表达式不包含显式方法调用,则调试器会评估它们并在“变量”视图中显示相应的值。

Basically, they are saying that the IDE will check the code around a breakpoint to identify the variables and compute their values (method calls not included). That being said, when the control reaches the line where accounts variableis declared, the IDE will check the code for variables and will found the acvariable, but will fail to compute its values since at the execution point that variable isn't declared yet, hence the entire operation will end with the message that the variable cannot be found.
To fix this, you have to uncheck that option from settings or you can leave it just like this, since it will not affect your code (it is fully functional right now).

基本上,他们说 IDE 将检查断点周围的代码以识别变量并计算它们的值(不包括方法调用)。话虽如此,当控件到达accounts variable声明的行时,IDE 将检查变量的代码并找到该ac变量,但将无法计算其值,因为在执行点该变量尚未声明,因此整个操作将以找不到变量的消息结束。
要解决此问题,您必须从设置中取消选中该选项,或者您可以保持原样,因为它不会影响您的代码(它现在功能齐全)。

回答by Godwin

Check is there any accounts are currently logged in.

检查当前是否有任何帐户登录。

Go to settings in that there is Accounts. check how many accounts are logged in.

转到设置,那里有帐户。检查有多少帐户登录。