title: Android - FragmentManager
body: <p>I am trying to make a little program which allows user to add items into a shopping list. The problem is that the program starts and closes immediately after a short while with the message: "Unfortunately, shopping has stopped" Shopping is the name of the app. What can be the reason?
  Here are all errors from LogCat in Eclipse:</p>
  
  <pre><code>AndroidRuntime(800): Shutting down VM
  
  W/dalvikvm(800): threadid=1: thread exiting with uncaught exception (group=0x41465700)
  
  E/AndroidRuntime(800): FATAL EXCEPTION: main
  
  E/AndroidRuntime(800): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shopping/com.example.shopping.MainActivity}: java.lang.NullPointerException
  
  E/AndroidRuntime(800):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
  
  E/AndroidRuntime(800):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
  
  E/AndroidRuntime(800):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
  
  E/AndroidRuntime(800):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
  
  E/AndroidRuntime(800):  at android.os.Handler.dispatchMessage(Handler.java:99)
  
  E/AndroidRuntime(800):  at android.os.Looper.loop(Looper.java:137)
  
  E/AndroidRuntime(800):  at android.app.ActivityThread.main(ActivityThread.java:5103)
  
  E/AndroidRuntime(800):  at java.lang.reflect.Method.invokeNative(Native Method)
  
  E/AndroidRuntime(800):  at java.lang.reflect.Method.invoke(Method.java:525)
  
  E/AndroidRuntime(800):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  
  E/AndroidRuntime(800):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  
  E/AndroidRuntime(800):  at dalvik.system.NativeStart.main(Native Method)
  
  E/AndroidRuntime(800): Caused by: java.lang.NullPointerException
  
  E/AndroidRuntime(800):  at com.example.shopping.ShoppingFragment.onCreateView(ShoppingFragment.java:31)
  
  E/AndroidRuntime(800):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
  
  E/AndroidRuntime(800):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
  
  E/AndroidRuntime(800):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
  
  E/AndroidRuntime(800):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
  
  E/AndroidRuntime(800):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
  
  E/AndroidRuntime(800):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
  
  E/AndroidRuntime(800):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
  
  E/AndroidRuntime(800):  at android.app.Activity.performStart(Activity.java:5143)
  
  E/AndroidRuntime(800):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
  
  E/AndroidRuntime(800):  ... 11 more
  
  I/Process(800): Sending signal. PID: 800 SIG: 9
  </code></pre>
  
  <p>And here is my code but I can't see any mistakes here:</p>
  
  <p>MainActivity file:</p>
  
  <pre><code>package com.example.shopping;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  
  public class MainActivity extends FragmentActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          FragmentManager fragManager = getSupportFragmentManager();
  
          Fragment theFragment = fragManager.findFragmentById(R.id.FragmentContainer);
  
          if (theFragment == null) {
              theFragment = new ShoppingFragment();
  
              fragManager.beginTransaction().add(R.id.FragmentContainer, theFragment).commit();
          }   
      }
  }
  </code></pre>
  
  <p>ShoppingFragment file:</p>
  
  <pre><code>package com.example.shopping;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.text.Editable;
  import android.text.TextWatcher;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  
  public class ShoppingFragment extends Fragment {
  
      private Shopping shopping;
      private EditText itemNameEditText;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          shopping = new Shopping();
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          View theView = inflater.inflate(R.layout.fragment_list, container, false);
          itemNameEditText = (EditText) theView.findViewById(R.id.itemNameEditText);
          itemNameEditText.addTextChangedListener(new TextWatcher() {
  
              @Override
              public void afterTextChanged(Editable s) {
              }
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count,
                      int after) {
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before,
                      int count) {
  
                  shopping.setName(s.toString()); 
              }
          });
  
          return theView;
      }
  }
  </code></pre>
  
  <p>I have really no idea why it doesn't want to work so thank you for any suggestions.</p>