0

I am going through the Android tutorial here. I have no problem implementing it the way they have. But, I am trying to understand why this other way fails. If I enable the two commented out lines and comment out the lines after them as noted, my app crashes. It seems to me that I should be able to reference an existing text view by ID, set its text, and then set the content view to be the layout that contains the text view I have referenced. I am sure I am thinking about this the wrong way, but I'd like some clarification on why it doesn't work.

    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
      //TextView textView = (TextView)findViewById(R.id.text_view);
        TextView textView = new TextView(this); //comment this out
        textView.setTextSize(40);
        textView.setText(message);
      //setContentView(R.layout.activity_display_message);
        setContentView(textView); //comment this out

and my xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.<myname>.myfirstapp.DisplayMessageActivity">

    <TextView
        android:id="@+id/text_view"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />

</RelativeLayout>
gfree
  • 479
  • 7
  • 16
  • 1
    Try getting the intent after setting the content view. – Benyam Ephrem Mar 13 '15 at 03:11
  • Benyam, your suggestion to put the intent after setting the content view worked. My follow up questions are 1) why does this make it work? My guess is it has something to do with the text_view object and when it is created. 2) Is there any benefit to doing it the tutorial way vs the way I have tried it? – gfree Mar 13 '15 at 03:24
  • 1) Intents cannot be placed before setContentView is called on a layout file. It doesn't have to do with the textview object. 2) There really is no "better" way, to me it is just preference to how you structure your code, but you should look deeper into this and intents as I'm no expert and you may learn more as intents are very important to Android – Benyam Ephrem Mar 13 '15 at 03:50

1 Answers1

1

It seems to me that I should be able to reference an existing text view by ID, set its text, and then set the content view to be the layout that contains the text view I have referenced

The logic is wrong. Only after setContentView() is called, the findViewById() can be used to get view from layout file, and then the view's methods can be called.

Your intent.getStringExtra(MyActivity.EXTRA_MESSAGE) can be called anywhere in OnCreate(), so there is nothing wrong about its position.

Terry Liu
  • 601
  • 5
  • 8
  • Awesome, I assumed it had something to do with when objects are created. This implies the TextView object in my XML doesn't exist until the content view is set. Am I correct in my understanding? – gfree Mar 13 '15 at 15:22
  • Yes, close to truth. The function of **setContentView(R.layout.xxx)** is loading the "structured views" in xml file to the activity's "view tree". **findViewById()** actually find the specified view in the "view tree" of activity, so it must be called after **setContentView(xxx)**, hope it can help you understand. – Terry Liu Mar 14 '15 at 13:01