Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView.setText (Android) is causing crashes.. any idea why?

Trying to get started with Android development, and doing some basic work with TextViews..

For some reason TextView's setText() method is causing huge problems for me.. here's a simplified version of my code to show what I mean:

package com.example.testapp;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        text = (TextView) findViewById(R.id.text1);  
        setContentView(R.layout.activity_main);
        text.setText("literally anything");
    }
}

This will cause a crash, and I don't understand why.. if I create the TextView within the onCreate it works just fine, but if I create it outside of it, it doesn't.. why is that? Has the line "TextView text;" not been executed yet or something?

Thanks!

like image 227
Zeldarulah Avatar asked Aug 08 '13 20:08

Zeldarulah


People also ask

What is the use of setText in android?

SetText(String, TextView+BufferType) Sets the text to be displayed using a string resource identifier.

What is main purpose of TextView?

A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.

How do I update TextView?

If you have a new text to set to the TextView , just call textView. setText(newText) , where newText is the updated text. Call this method whenever newText has changed.

Which method is used to set the text in a TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute.


4 Answers

You need to call setContentView() before initializing the TextView so that your Activity has access to all the layout components.

setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text1);  

text.setText("literally anything");
like image 190
Philipp Jahoda Avatar answered Oct 01 '22 16:10

Philipp Jahoda


switch these 2 lines

text = (TextView) findViewById(R.id.text1);  
    setContentView(R.layout.activity_main);

you need to set the content first

like image 38
tyczj Avatar answered Oct 01 '22 16:10

tyczj


From docs:

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

So this means that if you will reference your views in the layout, you must first set the content view and already then call findViewById method to reference child views of the layout resource defining your activity's UI

like image 22
Boris Mocialov Avatar answered Oct 01 '22 14:10

Boris Mocialov


text = (TextView) findViewById(R.id.text1);  
setContentView(R.layout.activity_main);
text.setText("literally anything");

If "literally anything" is a variable, which often may be the case, be sure that it isn't throwing a NullPointerException. I kept having that problem myself. I fixed it to be:

    text = (TextView) findViewById(R.id.text1);  
    setContentView(R.layout.activity_main);
    try {
         text.setText("literally anything");
    } catch (NullPointerException e) {
         // Do something
    }

Exceptions can be really useful, so if you're a beginning programmer, I suggest you put exception handling on your list of things to learn soon.

like image 36
Astrolamb Avatar answered Oct 01 '22 16:10

Astrolamb