Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

runOnUiThread doesn't work

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.

like image 326
hobbyprogrammer Avatar asked Sep 01 '25 02:09

hobbyprogrammer


1 Answers

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.

like image 59
Aleks G Avatar answered Sep 02 '25 14:09

Aleks G