title: Why is the On Click Listener Crashing my Android application?
body: <p>So, as a bit of background, I am currently an iOS developer, and I wanted to branch out and develop for android as well, and I finally found time to do so.</p>
  
  <p>I've been watching some video tutorials on developing for android, and in the lesson we made a simple app that when you click one button it adds 1 and when you click another it subtracts.</p>
  
  <p>There's also a small textview which the number value of the total changes as well when the button is clicked, all of which can be easily identified in the code.</p>
  
  <p>However, when I build and run the app, it displays an error on the screen saying "Unfortunately (Name of app) has stopped."</p>
  
  <p>Add upon debugging the app only stops working and displays this message after I add my first On Click Listener method.</p>
  
  <p><strong>Here is the main XML:</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      &gt;
  
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Your total is 0" 
          android:textSize="45dp"
          android:layout_gravity="center"
          android:gravity="center"
          android:id="@+id/tvDisplay"
  /&gt;
      &lt;Button 
      android:layout_width="250dp"
      android:layout_height="wrap_content"
      android:text="Add One"
      android:layout_gravity="center"
      android:textSize="20dp"
      android:id="@+id/bAdd"
  /&gt;     
  
      &lt;Button 
      android:layout_width="250dp"
      android:layout_height="wrap_content"
      android:text="Subtract One"
      android:layout_gravity="center"
      android:textSize="20dp"
      android:id="@+id/bSub"
  /&gt;   
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Here is the Manifest XML:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tutorial.thenewboston"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="13"
          android:targetSdkVersion="19" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.tutorial.thenewboston.StartingPoint"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>Here is the Java:</strong></p>
  
  <pre><code>public class StartingPoint extends ActionBarActivity {
  
  int counter;
  Button add, sub;
  TextView display;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_starting_point);
      counter = 0;
      add = (Button) findViewById(R.id.bAdd);
      sub = (Button) findViewById(R.id.bSub);
      display = (TextView) findViewById(R.id.tvDisplay);
      add.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
              counter++;
              display.setText("Your total is " + counter);
  
  
          }
      });
      sub.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              counter--;
              display.setText("Your total is " + counter);
  
  
          }
      });
  
      if (savedInstanceState == null) {
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.container, new PlaceholderFragment()).commit();
      }
  }
  </code></pre>
  
  <p><strong>Here is the LogCat:</strong></p>
  
  <pre><code>03-14 14:53:41.111: D/AndroidRuntime(17884): Shutting down VM
  03-14 14:53:41.111: W/dalvikvm(17884): threadid=1: thread exiting with uncaught exception (group=0x41ce4ba8)
  03-14 14:53:41.111: E/AndroidRuntime(17884): FATAL EXCEPTION: main
  03-14 14:53:41.111: E/AndroidRuntime(17884): Process: com.tutorial.thenewboston, PID: 17884
  03-14 14:53:41.111: E/AndroidRuntime(17884): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tutorial.thenewboston/com.tutorial.thenewboston.StartingPoint}: java.lang.NullPointerException
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.os.Handler.dispatchMessage(Handler.java:102)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.os.Looper.loop(Looper.java:136)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.app.ActivityThread.main(ActivityThread.java:5017)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at java.lang.reflect.Method.invoke(Method.java:515)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at dalvik.system.NativeStart.main(Native Method)
  03-14 14:53:41.111: E/AndroidRuntime(17884): Caused by: java.lang.NullPointerException
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at com.tutorial.thenewboston.StartingPoint.onCreate(StartingPoint.java:31)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.app.Activity.performCreate(Activity.java:5231)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  03-14 14:53:41.111: E/AndroidRuntime(17884):    ... 11 more
  </code></pre>
  
  <p><strong>Console Log:</strong></p>
  
  <pre><code>[2014-03-14 15:09:31 - TheNewBoston] ------------------------------
  [2014-03-14 15:09:31 - TheNewBoston] Android Launch!
  [2014-03-14 15:09:31 - TheNewBoston] adb is running normally.
  [2014-03-14 15:09:31 - TheNewBoston] Performing com.tutorial.thenewboston.StartingPoint activity launch
  [2014-03-14 15:09:31 - TheNewBoston] Automatic Target Mode: using device '015d483bce140216'
  [2014-03-14 15:09:31 - TheNewBoston] Application already deployed. No need to reinstall.
  [2014-03-14 15:09:31 - TheNewBoston] Starting activity com.tutorial.thenewboston.StartingPoint on device 015d483bce140216
  [2014-03-14 15:09:32 - TheNewBoston] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.tutorial.thenewboston/.StartingPoint }
  </code></pre>
  
  <p>And here is some more hopefully relevant information:
  I'm testing my apps on my personal Android Tablet not the Simulator, because the sim is dirt slow.
  My tablet is an Asus Nexus 7, with NVIDIA Tegra 3 1.20GHz
  1GB RAM, 32GB Flash Storage
  and is running Kit Kat, 4.4.2</p>
  
  <p><em><strong>EDIT:</em></strong></p>
  
  <p>My previous java contained an error which was linking it to my main XML view.
  it was calling activity_starting_point when it should have been calling fragment_starting_point.</p>
  
  <p><strong>This is the new code</strong></p>
  
  <pre><code>public class StartingPoint extends ActionBarActivity {
  
      int counter;
      Button add, sub;
      TextView display;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_starting_point);
          add = (Button) findViewById(R.id.bAdd);
          sub = (Button) findViewById(R.id.bSub);
          display = (TextView) findViewById(R.id.tvDisplay);
          add.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
                  counter++;
                  display.setText("Your total is " + counter);
  
  
              }
          });
          sub.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  counter--;
                  display.setText("Your total is " + counter);
  
  
              }
          });
  
          if (savedInstanceState == null) {
              getSupportFragmentManager().beginTransaction()
                      .add(R.id.container, new PlaceholderFragment()).commit();
          }
      }
  </code></pre>
  
  <p>However my problem is the exact same, the app crashes with the same message as before, but it does contain new logcat info:</p>
  
  <pre><code>03-14 15:25:44.621: E/FragmentManager(20269): No view found for id 0x7f05003c (com.tutorial.thenewboston:id/container) for fragment PlaceholderFragment{4247a988 #0 id=0x7f05003c}
  03-14 15:25:44.621: E/FragmentManager(20269): Activity state:
  03-14 15:25:44.621: D/FragmentManager(20269):   Local FragmentActivity 4245cf60 State:
  03-14 15:25:44.621: D/FragmentManager(20269):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
  03-14 15:25:44.621: D/FragmentManager(20269):     mLoadersStarted=false
  03-14 15:25:44.621: D/FragmentManager(20269):   Active Fragments in 4245e0b8:
  03-14 15:25:44.621: D/FragmentManager(20269):     #0: PlaceholderFragment{4247a988 #0 id=0x7f05003c}
  03-14 15:25:44.621: D/FragmentManager(20269):       mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
  03-14 15:25:44.621: D/FragmentManager(20269):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
  03-14 15:25:44.621: D/FragmentManager(20269):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
  03-14 15:25:44.621: D/FragmentManager(20269):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
  03-14 15:25:44.621: D/FragmentManager(20269):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
  03-14 15:25:44.621: D/FragmentManager(20269):       mFragmentManager=FragmentManager{4245e0b8 in StartingPoint{4245cf60}}
  03-14 15:25:44.621: D/FragmentManager(20269):       mActivity=com.tutorial.thenewboston.StartingPoint@4245cf60
  03-14 15:25:44.621: D/FragmentManager(20269):   Added Fragments:
  03-14 15:25:44.621: D/FragmentManager(20269):     #0: PlaceholderFragment{4247a988 #0 id=0x7f05003c}
  03-14 15:25:44.621: D/FragmentManager(20269):   FragmentManager misc state:
  03-14 15:25:44.621: D/FragmentManager(20269):     mActivity=com.tutorial.thenewboston.StartingPoint@4245cf60
  03-14 15:25:44.621: D/FragmentManager(20269):     mContainer=android.support.v4.app.FragmentActivity$2@4245ea30
  03-14 15:25:44.621: D/FragmentManager(20269):     mCurState=2 mStateSaved=false mDestroyed=false
  03-14 15:25:44.621: D/FragmentManager(20269):   View Hierarchy:
  03-14 15:25:44.621: D/FragmentManager(20269):     com.android.internal.policy.impl.PhoneWindow$DecorView{424636b0 V.E..... ... 0,0-0,0}
  03-14 15:25:44.621: D/FragmentManager(20269):       com.android.internal.widget.ActionBarOverlayLayout{424646a0 V.ED.... ... 0,0-0,0 #1020313 android:id/action_bar_overlay_layout}
  03-14 15:25:44.621: D/FragmentManager(20269):         android.widget.FrameLayout{42465448 V.E..... ... 0,0-0,0 #1020002 android:id/content}
  03-14 15:25:44.631: D/FragmentManager(20269):           android.widget.LinearLayout{42472528 V.E..... ... 0,0-0,0}
  03-14 15:25:44.631: D/FragmentManager(20269):             android.widget.TextView{42472840 V.ED.... ... 0,0-0,0 #7f05003d app:id/tvDisplay}
  03-14 15:25:44.631: D/FragmentManager(20269):             android.widget.Button{42472e30 VFED..C. ... 0,0-0,0 #7f05003e app:id/bAdd}
  03-14 15:25:44.631: D/FragmentManager(20269):             android.widget.Button{42473838 VFED..C. ... 0,0-0,0 #7f05003f app:id/bSub}
  03-14 15:25:44.631: D/FragmentManager(20269):         com.android.internal.widget.ActionBarContainer{42465960 V.ED.... ... 0,0-0,0 #1020314 android:id/action_bar_container}
  03-14 15:25:44.631: D/FragmentManager(20269):           com.android.internal.widget.ActionBarView{42465fb8 V.E..... ... 0,0-0,0 #1020315 android:id/action_bar}
  03-14 15:25:44.631: D/FragmentManager(20269):             android.widget.LinearLayout{42466718 V.....C. ... 0,0-0,0}
  03-14 15:25:44.631: D/FragmentManager(20269):               com.android.internal.widget.ActionBarView$HomeView{42467a90 V.E..... ... 0,0-0,0}
  03-14 15:25:44.631: D/FragmentManager(20269):                 android.widget.ImageView{42467f58 G.ED.... ... 0,0-0,0 #102025a android:id/up}
  03-14 15:25:44.631: D/FragmentManager(20269):                 android.widget.ImageView{42469038 V.ED.... ... 0,0-0,0 #102002c android:id/home}
  03-14 15:25:44.631: D/FragmentManager(20269):               android.widget.LinearLayout{4246a3f0 G.E..... ... 0,0-0,0}
  03-14 15:25:44.631: D/FragmentManager(20269):                 android.widget.TextView{4246a890 V.ED.... ... 0,0-0,0 #1020265 android:id/action_bar_title}
  03-14 15:25:44.631: D/FragmentManager(20269):                 android.widget.TextView{4246b578 G.ED.... ... 0,0-0,0 #1020266 android:id/action_bar_subtitle}
  03-14 15:25:44.631: D/FragmentManager(20269):           com.android.internal.widget.ActionBarContextView{4246bc60 G.E..... ... 0,0-0,0 #1020316 android:id/action_context_bar}
  03-14 15:25:44.631: D/FragmentManager(20269):         com.android.internal.widget.ActionBarContainer{4246c248 G.ED.... ... 0,0-0,0 #1020317 android:id/split_action_bar}
  03-14 15:25:44.631: D/AndroidRuntime(20269): Shutting down VM
  03-14 15:25:44.631: W/dalvikvm(20269): threadid=1: thread exiting with uncaught exception (group=0x41ce4ba8)
  03-14 15:25:44.631: E/AndroidRuntime(20269): FATAL EXCEPTION: main
  03-14 15:25:44.631: E/AndroidRuntime(20269): Process: com.tutorial.thenewboston, PID: 20269
  03-14 15:25:44.631: E/AndroidRuntime(20269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tutorial.thenewboston/com.tutorial.thenewboston.StartingPoint}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.tutorial.thenewboston:id/container) for fragment PlaceholderFragment{4247a988 #0 id=0x7f05003c}
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.os.Handler.dispatchMessage(Handler.java:102)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.os.Looper.loop(Looper.java:136)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.app.ActivityThread.main(ActivityThread.java:5017)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at java.lang.reflect.Method.invoke(Method.java:515)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at dalvik.system.NativeStart.main(Native Method)
  03-14 15:25:44.631: E/AndroidRuntime(20269): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.tutorial.thenewboston:id/container) for fragment PlaceholderFragment{4247a988 #0 id=0x7f05003c}
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.app.Activity.performStart(Activity.java:5241)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
  03-14 15:25:44.631: E/AndroidRuntime(20269):    ... 11 more
  </code></pre>