title: Android Studio Dynamically Appending a TextView to a ListView
body: <p>I am trying to do something similar to this chat in this tutorial (<a href="https://github.com/ScaleDrone/android-chat-tutorial" rel="nofollow noreferrer">https://github.com/ScaleDrone/android-chat-tutorial</a>), but I don't want to send sms messages. </p>
  
  <p>Basically I want this program to have a listview of messages (just like the tutorial that has both messages from myself but from another person. Like you're texting someone). </p>
  
  <p>It seems that the error comes from initializing the message_Body like in the java class.</p>
  
  <pre><code>TextView myMessage = (TextView) findViewById(R.id.message_body);
  </code></pre>
  
  <p>When I debug it says that myMessage is null. Is there anyone that might be able to help me with debugging this issue? Thanks!</p>
  
  <p>my_messages.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:paddingVertical="10dp"
  android:paddingRight="15dp"
  android:paddingLeft="60dp"
  android:clipToPadding="false"&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/message_body"
      android:background="@drawable/my_message"
      android:textColor="#fff"
      android:padding="10dp"
      android:elevation="2dp"
      android:textSize="18dp"
      android:layout_alignParentRight="true"
      android:text="Placeholder message"
      /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>their_message.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:paddingVertical="10dp"
  android:paddingLeft="15dp"
  android:paddingRight="60dp"
  android:clipToPadding="false"&gt;
  
  &lt;View
      android:id="@+id/avatar"
      android:layout_alignParentLeft="true"
      android:scaleType="centerInside"
      android:background="@drawable/circle"
      android:layout_width="34dp"
      android:layout_height="34dp" /&gt;
  
  &lt;TextView
      android:id="@+id/name"
      android:layout_marginLeft="15dp"
      android:layout_toRightOf="@+id/avatar"
      android:layout_alignTop="@+id/avatar"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingBottom="4dp"
      android:text="Rose"/&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/message_body"
      android:layout_below="@+id/name"
      android:layout_alignLeft="@+id/name"
      android:background="@drawable/their_message"
      android:paddingVertical="12dp"
      android:paddingHorizontal="16dp"
      android:elevation="2dp"
      android:textSize="18dp"
      android:text="How are you doing? This is a long message that should probably wrap."
      /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>In my java class I have this</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_detail);
  
      ListView view = (ListView) findViewById(R.id.mathListView);
      Intent in = getIntent();
      int index = in.getIntExtra("com.example.mathhack.ITEM_INDEX", -1);
  
      if (index &gt; -1) {
          TextView myMessage = (TextView) findViewById(R.id.message_body);
          List&lt;TextView&gt; textViewList = new ArrayList&lt;TextView&gt;();
  
          ListAdapter adapter = new ArrayAdapter&lt;TextView&gt;(this, android.R.layout.simple_list_item_1, textViewList);
          myMessage.setText("test1");
  
          textViewList.add(myMessage);
          view.setAdapter(adapter);
      }
  }
  </code></pre>