Andreas Wik
Andreas Wik

Reputation: 99

Android dev - TextView won't show up

I just started experimenting with Android app development and so I decided to give Android's own tutorials a go (this one: http://developer.android.com/training/basics/firstapp/starting-activity.html )

The textview in my new activity just won't show. Here's my code:

public class DisplayMessageActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Get the intent and the message sent with it */
        Intent intent = getIntent();
        String message = intent.getStringExtra(GoogleTutActivity.EXTRA_MESSAGE);

        /* Create a new textview where we can show the message the user sent */
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(R.layout.activity_display_message);
    }
}

Upvotes: 2

Views: 1950

Answers (2)

Rich Dudka
Rich Dudka

Reputation: 41

I just did this tutorial as a refresher and ran into this same problem. The tutorial does not explicitly state to delete the line setContentView(R.layout.activity_display_message), so I ended up having two calls to setContentView(). That's not allowed.

Two solutions will work. The easiest way to make this demo work is to delete the setContentView(R.layout.activity_display_message) line. Then the activity will have only one setContentView(), i.e. setContentView(textView) declared at the end of onCreate. That will use the programatic TextView a few lines above it.

A better solution is to create a layout for this activity in the res/layout folder and put the TextView details there rather than in onCreate().

Upvotes: 2

silentnuke
silentnuke

Reputation: 2622

you didn't add the textview to layout.
1. setContentView(textView);
2. or add textview to the xml activity_display_message and set id. then

 TextView textView = (TextView) findViewById(R.id.your_textview_id);
 textView.setTextSize(40);
 textView.setText(message);

Upvotes: 2

Related Questions