skip to Main Content

I’m trying to create a new Thread that updates a TextView’s value every 1 second and for some reason this code crashes the app

new Thread(){
        @Override
        public void run() {
            super.run();
            try {
                while(!this.isInterrupted()){
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setContentView(R.layout.activity_main);
                            TextView tvLegajo = (TextView)(findViewById(R.id.txtLegajo));
                            TextView tvNFabrica = (TextView)(findViewById(R.id.txtNFabrica));
                            if(App.getInstance().getLegajoAlumno()!=0)
                                tvLegajo.setText(App.getInstance().getLegajoAlumno());
                            else
                                tvLegajo.setText("");
                            tvNFabrica.setText(App.getInstance().getnFabricaNotebook());
                        }
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();

That is in the onCreate function in my MainActivity
Just to clarify, I’m trying to update the values in the Views

R.id.txtLegajo 
R.id.txtNFabrica

to the values in

App.getInstance().getLegajoAlumno();
App.getInstance().getNFabricaNotebook();

(also if the …getLegajoAlumno() is 0, R.id.txtLegajo is set to a blank String)

EDIT:

I did the changes recomended by @DiLDoST but it crashes regardles.
A detail is that the thread is created in the onCreate method of the activity.
The logcat after the crash looks like this:

2022-09-12 10:50:39.696 11067-11067/com.example.prestaciones E/le.prestacione: Invalid ID 0x000368bd.
2022-09-12 10:50:39.696 11067-11067/com.example.prestaciones D/AndroidRuntime: Shutting down VM
2022-09-12 10:50:39.697 11067-11067/com.example.prestaciones E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.prestaciones, PID: 11067
    android.content.res.Resources$NotFoundException: String resource ID #0x368bd
        at android.content.res.Resources.getText(Resources.java:367)
        at android.widget.TextView.setText(TextView.java:6370)
        at com.example.prestaciones.MainActivity$1$1.run(MainActivity.java:47)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2022-09-12 10:50:39.720 11067-11067/com.example.prestaciones I/Process: Sending signal. PID: 11067 SIG: 9

2

Answers


  1. ISSUE: I guess your issue is calling setContentView again and again…

    FIX: setContentView and assign Variables in onCreate:

    TextView tvLegajo;
    TextView tvNFabrica;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvLegajo = (TextView)(findViewById(R.id.txtLegajo));
        tvNFabrica = (TextView)(findViewById(R.id.txtNFabrica));
    }
    

    Now start your thread:

        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    while(!this.isInterrupted()){
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(App.getInstance().getLegajoAlumno()!=0)
                                        tvLegajo.setText(App.getInstance().getLegajoAlumno());
                                    else
                                        tvLegajo.setText("");
                                    tvNFabrica.setText(App.getInstance().getnFabricaNotebook());
                                }
                            });
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    

    PERFECT CURE: Its better if you show us the error from logcat, so we can help to fix it in a better way.

    Login or Signup to reply.
  2. Your tvLegajo.setText(App.getInstance().getLegajoAlumno()); is being resolved to a number. This means the setText method is thinking it’s a Resource Id. Because it’s not, then you get a Resource Not Found exception.

    Instead make it a string: getLegajoAlumno().toString().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search