Reputation: 65
This is one of my first apps, and I don't know what I have to do.
package com.example.stopuhr;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int hs, sek, min;
public boolean running = false;
public String mode = "gestoppt";
public void setLabelText(){
TextView textView1 = (TextView)findViewById(R.id.textView1);
String ht = "";
String st = "";
String mt = "";
if (hs<10){
ht = "0" + hs;
}else{
ht = String.valueOf(hs);
}
if (sek<10){
st = "0" + sek;
}else{
st = String.valueOf(sek);
}
if (min<10){
mt = "0" + min;
}else{
mt = String.valueOf(min);
}
textView1.setText(mt + " : " + st + " : " + ht);
}
public void onClickStart (View view){
Thread timer = new Thread(){
Here's are the first errors Multiple markers at this line - Syntax error, insert "}" to complete ClassBody - Syntax error, insert ";" to complete ConstructorDeclaration - Syntax error, insert ";" to complete BlockStatements - Return type for the method is missing - Syntax error, insert ")" to complete ConstructorDeclaration
runOnUiThread(new Runnable() {
public void run(){
if (mode.equals("gestoppt")){
running = true;
mode = "läuft";
while (running){
try{Thread.sleep(9);}
catch(Exception e){}
if(hs<=99){
hs++;
}else{
hs = 0;
if(sek<=59){
sek++;
}else{
sek = 0;
}
if(min<=99){
min++;
}else{
min = 0;
}
}
}
setLabelText();
}
}
Here is the second mistake: Syntax error on token "}", MethodHeaderName expected I don't know what I have to do with this error.
});
};
timer.start();
}
public void onClickStop (View view){
if (mode.equals("läuft"));
running = false;
mode = "gestoppt";
}
public void onClickReset (View view){
if(mode.equals("gestoppt")){
hs = 0;
sek = 0;
min = 0;
setLabelText();
}
}
}
Thank you for your help.
Upvotes: 4
Views: 9303
Reputation: 52376
It looks like there is a Thread.sleep in your code that is blocking the UI. This might stop the UI from updating.
Upvotes: 0
Reputation: 166
I think it is better to put your functionality into a method in the MainActivity
class:
private void doMyOperation() {
runOnUiThread(.....);
}
and call that method in your thread.
Upvotes: 0
Reputation: 57316
In your code you have:
public void onClickStart (View view){
Thread timer = new Thread(){
runOnUiThread(new Runnable() {
public void run() {
...
}
});
};
}
The problem here is that you have the call runOnUiThread
just inside a class - not in a method. In general you seem to be confused with threads, runnable and runOnUi. There's no point starting a new thread if you then want to invoke its run
method on UI thread. The only thing you need to do on the UI thread is update the label text. Without going through your logic, one way of fixing the syntax errors would be:
public void onClickStart (View view) {
Thread timer = new Thread(){
public void run() {
...
runOnUiThread(new Runnable() {
public void run() {
setLabelText();
}
}
};
timer.start();
}
Finally, note that this is not the best way of performing this kind of logic. An AsyncTask
with onProgressUpdate
seems to me a much better solution.
Upvotes: 3