简体   繁体   中英

why doesn't TextView set text work with int?

why doesn't TextView set text work with int? I tried to set text of a textview through an int variable and when the code tries to set the app crash

TextView pntUno = (TextView) findViewById(R.id.punteggioGiocatoreUno);
TextView pntDue = (TextView) findViewById(R.id.punteggioGiocatoreDue);
int pnt1 = 0, pnt2 = 0;

private void punteggio(){
    pntUno.setText(pnt1);
    pntDue.setText(pnt2);
}

The problem is that you are calling setText(int resid) instead of setText(CharSequence text) . You can take a look at the documentation to understand more.

To solve this, you can convert the int to String by using String.valueOf() :

private void punteggio(){
    pntUno.setText(String.valueOf(pnt1));
    pntDue.setText(String.valueOf(pnt2));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM