• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Changing text set by an XML

I completed this Linear Layout Tutorial and I was wanting to play around with it a bit. So I'm trying to change the text 'red' to something else. In the XML I added

Code:
android:id="@+id/red"

to the TextVeiw

and I'm trying to use

Code:
public class HelloLinear extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView newText;
        newText = (TextView) findViewById(R.id.red);
        newText.setText("red");
        
        setContentView(R.layout.main);
    }
}

to change the text on startup but it just throws up an error and crashes.

Thanks for the help.
 
Expanding on the last post, specifically in the spot:

Code:
public class HelloLinear extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        [B]setContentView(R.layout.main);[/B]
        TextView newText;
        newText = (TextView) findViewById(R.id.red);
        newText.setText("red");
    }
}
 
I knew it was something basic. I did try putting setContent before setText, but I never thought that findView would be a problem.

So is the R file not even created before you call setContent?

Anyways thanks for the help.
 
No problem, have a good one.

And the R file is created on build when developing, but you cant add content to a view without telling the application what view you're looking at..

having setContentView() after making the TextView is like saying "then put the rock down, walk in the room" but you can rethink something that you heard, so when you're told "then put the rock down" you don't know where to put it, so you force close. The application doesn't know what you're referring to until a view is set, hope this helps!
 
Back
Top Bottom