I'm very new to Java programming and I'm having troubles with while loops in Threads. I post here an example where I use the while loop.
public class MainActivity extends AppCompatActivity {
private boolean running = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView)findViewById(R.id.textView);
new Thread(new Runnable() {
@Override
public void run() {
while (running) {
try{Thread.sleep(1000);}
catch(InterruptedException e){}
running = false;
text.setText("false!");
running = true;
try{Thread.sleep(1000);}
catch(InterruptedException e){}
text.setText("True!");
}
}
}).start();
}
}
When running is set to false, the Thread doesn't run and the app does not show anything. When running is set to true, the app crashes after text.setText("false!");
Thank you all in advance!