title: Android: Where to insert code in MainActivity
body: <p>My MainActivity looks like this:</p>
  
  <pre><code>import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  
  
  public class MainActivity extends FragmentActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main); 
  
  
      ViewPager myViewPager = (ViewPager) findViewById(R.id.viewPager);
      myViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
      myViewPager.setCurrentItem(1, false);
  }
  
  private class MyPagerAdapter extends FragmentPagerAdapter {
  
      public MyPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int pos) {
          switch(pos) {
  
          case 2: return FirstFragment.newInstance("FirstFragment, Instance 1");
          case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
          case 0: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
          default: return ThirdFragment.newInstance("ThirdFragment, Default");
          }
      }
  
      @Override
      public int getCount() {
          return 3;
      }       
  
  
      {}}}
  </code></pre>
  
  <p>and i want to insert this to add animation to some ImageButtons:</p>
  
  <pre><code>final ImageButton b = (ImageButton) findViewById(R.id.imageButton1);
      b.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View arg0) {
          Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
          b.startAnimation(anim);
      }
  });
  
      final ImageButton c = (ImageButton) findViewById(R.id.imageButton2);
      c.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View arg0) {
          Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
          c.startAnimation(anim);
      }
  });
  
      final ImageButton d = (ImageButton) findViewById(R.id.imageButton3);
      d.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View arg0) {
          Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
          d.startAnimation(anim);
      }
  });
  
  }
  
  }}
  </code></pre>
  
  <p>the animations work fine when separate but when i insert it to the MainActivity i get loads of errors, the problem is im not really sure where to insert this code without getting errors.</p>
  
  <p>i have tried inserting at the end of onCreate(), then i get no errors but instead the application crashes when launched?
  Here is the LogCat:</p>
  
  <pre><code>03-22 15:01:12.731: E/AndroidRuntime(28671): FATAL EXCEPTION: main
  03-22 15:01:12.731: E/AndroidRuntime(28671): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.os.Handler.dispatchMessage(Handler.java:99)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.os.Looper.loop(Looper.java:137)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.app.ActivityThread.main(ActivityThread.java:4867)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at dalvik.system.NativeStart.main(Native Method)
  03-22 15:01:12.731: E/AndroidRuntime(28671): Caused by: java.lang.NullPointerException
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at com.example.test.MainActivity.onCreate(MainActivity.java:21)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.app.Activity.performCreate(Activity.java:5047)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
  03-22 15:01:12.731: E/AndroidRuntime(28671):    ... 11 more
  </code></pre>