public class MainActivity extends AppCompatActivity { int y; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } class MyView extends View { public MyView(Context context) { super(context); } public void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setTextSize(20); canvas.drawColor(Color.BLACK); new CyclerThread().startCycle((i) -> y=i); canvas.drawText(""+ y,100,100, paint); } } } 

.................................................. .................

 public class CyclerThread { public interface Callback { void onValueChanged(int i); } public void startCycle(Callback callback) { Random random = null; for (int op = 0; op>0; op++) { boolean bo = random.nextBoolean(); if (bo == true) { for (int i = 0; i < 5; i++) { try { Thread.sleep(2000); // any action } catch (InterruptedException e) { e.printStackTrace(); } callback.onValueChanged(i); // System.out.println(i); } } else { try { Thread.sleep(5000); // any action } catch (InterruptedException e) { e.printStackTrace(); } } } } } 
  • And what do you really want from the code? I did not understand a bit what was happening in the cycle. - Rostislav Dugin
  • Well, in general, I need to change the value in the interval when the boolean истинна , then the cycle goes from one to five (i) in two-second increments, and if it lies then the whole cycle stops for five seconds, I need to output many such values, but of course I I will not write dozens of such cycles as the number of cycles will definitely be later and it will always be different - Jonathan
  • Going even deeper: why do you do this at all?) Maybe there is a better solution? - Rostislav Dugin
  • so I need to request a copy of the cycle and return the value - Jonathan
  • Unfortunately, no, this is the easiest way to solve my problem - Jonathan

1 answer 1

  1. I'm not at all sure that your code works. There are too many conflicting points. Do everything under a friend, not the way you do now.
  2. Your code does not work, because i may not be assigned at all.

Try this:

 new CyclerThread().startCycle((i) -> canvas.drawText(String.valueOf(i), 100, 100, paint)); 

Although I think this is a terrible decision.


I was not lazy and copied your code into the project. It simply does not work.

IDEA clearly says cycle:

for (int op = 0; op>0; op++)

ALWAYS false . Change it to op >= 0 or use while(true) , which is more preferable.


But your code will not work, because the class Random is null .

  • it's not even about assignment, output directly the String.valueOf (i) result is 0, so nothing is transferred from the class - Jonathan
  • @Jonathan, first the loop passes 0, then 1, then 2, and so on. - Rostislav Dugin
  • that's right but at the moment he does not transmit anything - Jonathan
  • yes, the loop is definitely not working, edited final random random = new Random (); and (int op = 1; op> 0; op ++) and the cycle worked, but only in the standard Java compiler, only the android application loads this option tightly - Jonathan
  • one
    I think the practical part is no less important, but I agree with the theory, it is not enough for me, but still I figured it out - it turned out that Canvas in which I tried to deduce values ​​from the loop does not draw until the loop is complete, and since the loop is endless Rendering Unbelieve)) - Jonathan