In Xcode I am able to "po" any values while debugging. Is there a way to do this in Android Studio?
For example, if I hit a breakpoint and want to print out certain values in realtime as opposed to having to use Logs in my code?
You can do the following steps:
1- Set BreakPoint in the line you want to evaluate Ctrl+F8.
2- Run your code in debug mode or press Shift+F9.
3- When reached to the BreakPoint press Alt+F8 or click on the following button.
4- Then type your value in text-field and click on the Evaluate button.
References:
https://www.jetbrains.com/help/idea/debugging-code.html
https://www.jetbrains.com/help/idea/evaluating-expressions.html
Use Watches. It gives you the ability to evaluate expressions.
When you hit debug point, there are two ways to check values :
(1) Add Watch for any variable
(2) Hover over any variable, the value will be displayed there
If it's a c/c++ project, you still can use lldb command in Android Studio.
Here's the official document. When you hit the breakpoint, you will see a tab named "lldb" next to the variables tab.
Related
I'm repairing my friend's code and got confused.
My friend wants to fetch entered text (in EditText). Seems easy, right? Well, it is but instead of user input, he gets this warning/error:
To be honest I'm not sure how to fix it. He is coding in Kotlin (Android 10).
Activity that contains EditText:
And XML:
This is how it looks when debugging:
The app started working just fine after running "File -> invalidate Cashes/Restart" option, I just don't understand where this warning came from and how to fix it because the error remained unchanged (even though the app works). Do you have an idea how to solve it?
All the best!
fyi lambda expression like setOnClickListener from kotlin is not debuggable, see here.
if you want to debug variables inside setOnClickListener you should use the normal one e.g. setOnClickListener(object: View.OnClickListener {..})
sometimes there will be problem in auto generated binding files, if so it will be solved after invalidate cache and restart ide. sometimes the warning/error show but the project and complied without errors. so no need to worry about that. for next time post the code as code not screen shots.
I understand that the question is regarding evaluating expression, but there is a way you can read variables from your debugger console, even if you're inside an anonymous callback. I found it helpful sometimes. Here are the steps:
First enter debugger mode inside of your anonymous callback,
In your debugger console, look at the right side for "Frames"
Within Frames under , you'll see stack of function execution first top one on the list is the latest:
Click on row(s) below the latest function, until you find an instance of your activity AddInformationActivity. You will see the activity instance on the right side window under Variables. Don't go as far as selecting functions browned out, because those are from internal libraries.
When you see you AddInformationActivity instance, you can expand it and see its variables.
Hope that helps!
It's not a beautiful way, but if you create a method like this:
private fun debug() {
println()
}
and add a breakpoint on the println() it'll capture the activity.
(Don't use TODO as it'll crash the app with a NotImplementedError once called.)
I have this method now in my code all the time to call it whenever I need it.
I know, that question is old, but I just stumbled over it and needed a way.
Is there any way to have conditional Debug in android studio?
for example I wanna go to debug mode when a specific variable value is.
for example:
for(MyClass myclass: ClassList){
String title = myclass.gettitle(); // this is where break point is
}
Before that I do not need the debug mode. I just wanna examine the app on that exact value. for example when title="KickBoxing" in this case I should check the variable value and if it is not my required value I should press F9 to go to next value. maybe this takes 50 times to get to my desired value.
It is possible to add a condition for a breakpoint. Add a breakpoint, as you normally do, and click with the right mouse button on it. You will be prompt with a dialog. In the condition you can put some java code.
E.g. you could put "kickBoxing".equals(title)
this way the debugger will stop on that breakpoint only when the condition is true.
(photo taken from http://javafortesters.com/)
You can do that with Android Studio:
Set a breakpoint on the line you need.
Right-click on the breakpoint
Paste your condition into the field named Condition
For example, your condition should be like myclass.gettitle().equals("KickBoxing")
What is the shortcut key to find that where is the particular global variable Initializing in android studio project.
Place your cursor on the variable and then press Alt+F7 for the Find Useages command. In the Find Usages result, look for "Value write". Expanding that category will show all the places where the variable is assigned, which includes initialization.
If you middle-mouse-click any of your variables it takes you to the delcaration. If you middle click it again, it pops up a window with every usage. Alt+F7 works too.
In Eclipse, I can put my cursor on a method name and hit ctrl-space and I get a list of available template proposals.
In Android Studio, when I'm typing a method name and open paren, I get a list of template proposals, but afterwards, how do I see the list again? I've already got show quick doc on mouse move, but that only shows the method signature I selected at first. I want to see alternatives to what I selected the first time through.
How do I show alternative method signatures, aka alternative template proposals?
EDIT:
Thanks to AndroidMechanic for answering Ctrl-P.
Here is what it looks like in Android Studio:
Here is what it looks like in Eclipse: . Notice that you're presented with the documentation in Eclipse, which I find helpful. But Ctrl-P gets me most of the way there.
In android studio to see all method overloads click inside the parenthesis and hit Ctrl + P. This will show you the different combinations you could pass as parameters to a method.
I know I could set a breakpoint at every line where my code changes the variable, but is there an option such as right-clicking a variable (to "Add to Watches") that would stop when a variable changes value?
I think C++ has this option. See this.
And Eclipse? See this.
Is this implemented in AS?
You can break on value changes of some variables in Android Studio 1.1.0.
Android Studio calls them 'Java Field Watchpoints'.
From the breakpoints windows, (Run -> "View breakpoints...") or Ctrl+Shift+F8,
you can add "Java Field Watchpoints" from the plus in the top left corner, and then select your class and variable.
If the goal is simply to set a watchpoint where execution halts if a variable's value changes, there are two methods:
Pressing Ctrl-shift-F8 to set a watchpoint brings up this dialog:
That looks more powerful but it's confusing because it lacks the option I need; and there's an extra step: filling in the dialog box below:
Once you fill it in correctly, you get the same options for stopping execution as in the simpler method below:
Define the variable on one line ending with a semicolon.
Left-click in the area where you set a normal breakpoint and up pops a dialog box.
Choose options to suit your needs.
Any instance where the value of the variable prefDbExists changes, execution will pause as at any "normal" (unconditional) breakpoint. And that's all I needed when I asked the original question.
EDIT: Fully qualified class name is {package name}.{class name})
What you seek for is commonly known as "WatchPoint".
You will find detailed answer and examples on the doc. page of JetBrains - IntelliJ - Idea which is the basis for Android Studio.
https://www.jetbrains.com/help/idea/2016.3/creating-field-watchpoints.html
For me this works perfectly !
Add a Kotlin Field Watchpoint by clicking on the space next to the line number for the field declaration in Android Studio. Select Kotlin Field Watchpoint from the Set Breakpoint menu. You will now get a breakpoint whenever that value changes.