1

Scenario : I have a variable and I want to find out where and when the variable gets for example the value 4. When this happens the debugger should stop at that line.

Is this possible with Android Studio ?

Ahmet K
  • 713
  • 18
  • 42

2 Answers2

7

If I understand correctly what you're wanting to do is set what is called a "Watchpoint" in Android Studio, and here's a link to the IntelliJ documentation that discusses them:

https://www.jetbrains.com/help/idea/2016.3/creating-field-watchpoints.html

In particular, what you want to do is set a break-point on the member variable itself, right-click that break-point and set the watch on "field modification" and set a condition for when that variable becomes the specific value you're trying to find.

So, in this simple bit of code:

public class MainActivity extends AppCompatActivity
{

int mWatchMe = -10;

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

        for (int i=0 ; i<20 ; i++)
        {
            mWatchMe++;
        }
    }
}

You would set a break-point on the line: int mWatchMe = -10;.

Set the "Condition" and "modification" fields:
enter image description here

The execution should break where ever "mWatchMe" is set to '0':
enter image description here

Floern
  • 33,559
  • 24
  • 104
  • 119
Slartibartfast
  • 591
  • 5
  • 18
2

This is possible. Right click on your breakpoint and then enter your expression "value == 4" in the condition field.

mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
  • But what when I dont know where to break ? If I do it like that it is maybe too late and this happened 10 lines before or so – Ahmet K Jun 11 '16 at 10:25
  • Well, you have to know at least where you need your breakpoint. Maybe you should give more information about your problem then. – mrkernelpanic Jun 11 '16 at 10:33
  • My Variable gets the value 0 and I dont know where. I cant just search for it because its changing in runtime – Ahmet K Jun 11 '16 at 10:35