like the title says I\'m getting an error when trying to set a text view text from java in my android project, I can\'t see why.
package com.codeherenow.
You've got a couple issues.
First, you can't do this here
public TextView YT = (TextView) findViewById(R.id.Years);
Because the layout hasn't been inflated yet so you can't look for your View
witht findViewById()
. You can declare it there
public TextView YT;
but you need to initialize it after you call setContentVie()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
//can't do it before that line ^
YT = (TextView) findViewById(R.id.Years);
}
And you are trying to call setText()
outside of a method. Move that to some method. But you can't do it in onCreate()
because Years
isn't given a value. So you probably want it somewhere else. Maybe in onProgressChanged()
.
Also, to follow Java naming conventions, you should name your variables by starting with lower case (yt or yT and years).