A
A
Alexey R2014-06-01 13:44:20
Java
Alexey R, 2014-06-01 13:44:20

How to display given value in TextView?

Good day to all. I have such a problem. I am unable to display the given value in the TextView. There is a str variable equal to 100, as soon as I try to display it in Anfdroid, the application immediately closes. In fact, it doesn't even start. Isn't it possible to deduce the value this way?

public class MainActivity extends Activity implements OnClickListener{
  Button button1;
  EditText edittext;
  TextView textview, textview2;

  public int str = 100;
  
  @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //Находим элементы по ID
        button1 = (Button) findViewById(R.id.button1);
        edittext = (EditText) findViewById(R.id.editText1);
        textview = (TextView) findViewById(R.id.textView1);
        textview2 = (TextView) findViewById(R.id.textView2);
            
        // Присваиваем размер депозита
        textview2.setText(str); - ЗДЕСЬ ВОЗНИКАЕТ ОШИБКА
  
}

Thank you all very much for your help!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FoxInSox, 2014-06-01
@Axeles

Logcat is great at pointing out the error. The line "Caused by ... Resources$NotFoundException: String resource ID #0x64" means that you are calling the following method:

public final void setText(int resid) {
        setText(getContext().getResources().getText(resid));
}

0x64 is the hexadecimal representation of your number 100. This method takes a resource ID as a parameter, not just the number you would like to display in the text field. Convert your number to a string:
Read Java Basics and Android Guide , otherwise you will stumble over such errors for a very long time.

Y
Yuri Petrashevich, 2014-06-01
@BelCoder

Check out - stackoverflow.com/questions/3275467/why-cant-i-set...

@
@breakmt, 2014-06-02
_

You are passing an ID to a resource that doesn't exist

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question