Home > Software design >  Android Studio Java app crashing when trying to update TextView
Android Studio Java app crashing when trying to update TextView

Time:09-10

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)

CodePudding user response:

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.

CodePudding user response:

Please share the the error in logcat or the stack trace!

  • Related