title: Trying create a chat with Fragment?
body: <p>I'm trying create a chat using Fragment. To do it I found an example on google. The project use Activity and I'm trying it to Fragment because my project all developed with Fragments. When I do change from Activity to Fragment this line: <code>adapter = new DiscussArrayAdapter(getView().getContext(), R.layout.listitem_discuss);</code> throws an exception </p>
  
  <p>Here how I'm trying</p>
  
  <p><strong>XML</strong></p>
  
  <pre><code>///activity_discuss
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
      &lt;ListView
          android:id="@+id/listView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="80dp"&gt;
      &lt;/ListView&gt;
  
      &lt;RelativeLayout
          android:id="@+id/form"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:orientation="vertical" &gt;
  
          &lt;EditText
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:inputType="textMultiLine"
              android:ems="10"
              android:id="@+id/chatText"
              android:layout_alignParentBottom="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:layout_toLeftOf="@+id/buttonSend" /&gt;
  
          &lt;Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Send"
              android:id="@+id/buttonSend"
              android:layout_alignBottom="@+id/chatText"
              android:layout_alignParentRight="true"
              android:layout_alignParentEnd="true" /&gt;
      &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  
  
  ///listitem_discuss
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" &gt;
  
      &lt;LinearLayout
          android:id="@+id/wrapper"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
  
          &lt;TextView
              android:id="@+id/comment"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:layout_margin="5dip"
              android:background="@drawable/bubble_yellow"
              android:paddingLeft="10dip"
              android:text="Hello bubbles!"
              android:textColor="@android:color/primary_text_light" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Fragment</strong></p>
  
  <pre><code>public class HelloBubblesActivity extends Fragment {
      private DiscussArrayAdapter adapter;
      private ListView lv;
      private LoremIpsum ipsum;
      private EditText editText1;
      private Button btnSend;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.activity_discuss, container, false);
          lv = (ListView)getView().findViewById(R.id.listView1);
  
          adapter = new DiscussArrayAdapter(getView().getContext(), R.layout.listitem_discuss);
          lv.setAdapter(adapter);
  
          editText1 = (EditText)view.findViewById(R.id.chatText);
          editText1.setOnKeyListener(new OnKeyListener() {
              public boolean onKey(View v, int keyCode, KeyEvent event) {
                  // If the event is a key-down event on the "enter" button
                  if ((event.getAction() == KeyEvent.ACTION_DOWN) &amp;&amp; (keyCode == KeyEvent.KEYCODE_ENTER)) {
                      // Perform action on key press
                      //adapter.add(new OneComment(false, editText1.getText().toString()));
                      receiveMessage();
                      editText1.setText("");
                      return true;
                  }
                  return false;
              }
          });
  
          btnSend = (Button)view.findViewById(R.id.buttonSend);
          btnSend.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  //adapter.add(new OneComment(false, editText1.getText().toString()));
                  receiveMessage();
                  editText1.setText("");
              }
          });
  
          return  view;
      }
  
  
  
  
      private void receiveMessage(){
          String msg = editText1.getText().toString();
          new ChatDAO().receiveMessage("");
      }
  
  
      /** recebe msg */
      private void addItems() {
          adapter.add(new OneComment(true, "Hello bubbles!"));
      }
  
  }
  </code></pre>
  
  <p><strong>Adapter</strong></p>
  
  <pre><code>public class DiscussArrayAdapter extends ArrayAdapter&lt;OneComment&gt; {
  
      private TextView countryName;
      private List&lt;OneComment&gt; countries = new ArrayList&lt;OneComment&gt;();
      private LinearLayout wrapper;
  
      public DiscussArrayAdapter(Context context, int resource) {
          super(context, resource);
      }
  
      @Override
      public void add(OneComment object) {
          countries.add(object);
          super.add(object);
      }
  
  
  
      public int getCount() {
          return this.countries.size();
      }
  
      public OneComment getItem(int index) {
          return this.countries.get(index);
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          View row = convertView;
          if (row == null) {
              LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              row = inflater.inflate(R.layout.listitem_discuss, parent, false);
          }
  
          wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
  
          OneComment coment = getItem(position);
  
          countryName = (TextView) row.findViewById(R.id.comment);
  
          countryName.setText(coment.comment);
  
          countryName.setBackgroundResource(coment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
          wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
  
          return row;
      }
  
      public Bitmap decodeToBitmap(byte[] decodedByte) {
          return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
      }
  
  }
  </code></pre>