title: Add a fragment to activity its not working
body: <p>Im studying about fragments and Im doing a basic example but its not working. </p>
  
  <p>I have a basic example where I have 2 activities:
  - One activity with a image and a textview and when the image is clicked I go to another activity that that has a list view with some items. When this list view is clicked I go back to the main activity and in the textview I show the text associated with the list item clicked.</p>
  
  <p>But Now Im trying to pass this basic example but using fragments with only one activity, but its not working it appears always “app has stopped” when the image is clicked.</p>
  
  <p>What Im doing, and I don’t know if its the best solution, is have the main activity just with the image that needs to be clicked so that it appears the fragment with the list view. And having 2 fragments, one to show the list view and another to show the textview with the text of the clicked item.</p>
  
  <p>For now im just doing the first part, that is click on the image and show the fragment with the list view and when the item on the fragment is clicked show a message with toast. But its not working, it appears always “app has “stopped”.</p>
  
  <p>So what I have is, in the main activity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private ImageView img;
  private TextView tv;
  Intent intent;
  
  FragmentManager fragmentManager;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      fragmentManager = getFragmentManager();
  
      img = (ImageView) findViewById(R.id.imageView);
      tv = (TextView) findViewById(R.id.textView);
  
  
  }
  
  
  public void AddFragmentList(View view) {
      Fragment fragment = new Fragment();
      FragmentTransaction transaction = fragmentManager.beginTransaction();
      transaction.add(R.id.listviewInFragment, fragment, "frag");
      transaction.commit();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>main activity xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;ImageView
          android:id="@+id/imageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:srcCompat="@drawable/image"
          android:onClick="AddFragmentList"
          tools:layout_editor_absoluteY="71dp"
          tools:layout_editor_absoluteX="0dp" /&gt;
  
  
      &lt;FrameLayout
          android:layout_width="0dp"
          android:layout_height="11dp"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintHorizontal_bias="0.0"
          app:layout_constraintBottom_toBottomOf="parent"
          android:layout_marginBottom="110dp"&gt;
  
      &lt;/FrameLayout&gt;
  
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>In fragment java:</p>
  
  <pre><code>public class fragment extends Fragment {
  
      private ListView listItems;
  
      private String[] items = {
              "item1",
              "item2",
              "item3",
              "item4"
      };
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment, container, false);
  
          return view;
  
          }
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
  
          listItems = (ListView) listItems.findViewById(R.id.listviewInFragment);
  
  
          ArrayAdapter&lt;String&gt; adapter =
                  new ArrayAdapter&lt;String&gt;(getActivity().getApplicationContext(),
                          android.R.layout.simple_list_item_1, android.R.id.text1, items);
  
          listItems.setAdapter(adapter);
  
          listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
                  int positionCode = i;
                  String clickedValue = (String) listItems.getItemAtPosition(positionCode);
  
                  Listener listener = (Listener) getActivity();
                  listener.addText(clickedValue);
  
                  Toast.makeText(getActivity().getApplicationContext(), clickedValue, Toast.LENGTH_SHORT).show();
  
              }
  
          });
      }
  }
  </code></pre>
  
  <p>And in fragment xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#0ff"&gt;
  
      &lt;ListView
          android:layout_width="368dp"
          android:layout_height="495dp"
          android:layout_marginLeft="8dp"
          android:layout_marginStart="16dp"
          android:id="@+id/listviewInFragment"/&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>