Is it possible to watch variables at runtime?

前端 未结 2 1620
刺人心
刺人心 2021-02-05 12:54

I know the basics of debugging, and I know I can add watches to a variable that stop the program\'s execution on a given condition. But I didn\'t want to stop the program every

2条回答
  •  忘了有多久
    2021-02-05 13:49

    If you know the basics of debugging, you can easily add watches to a variable that stop the program's execution on a given condition. If you didn't want to stop the program every time you want to see the value of a variable then the easy way to see the value of a variable is to use Toasts. A toast provides a sample value of any variable in an operation in a small popup. Toasts automatically disappear after a set timeout.

    A simple code example:

    Context context = getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;
    
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    

    In order to see the variable value in a Toast:

       int var=1;
       Toast.makeText(getApplicationContext(), "vlaue is "+var, Toast.LENGTH_LONG).show();
    

    In order to see the variable type in a Toast:

       Toast.makeText(getApplicationContext(), "type is "+var.getClass().getName(), Toast.LENGTH_LONG).show();
    

提交回复
热议问题