title: Null Pointer exception in Fragment on callback from Activity
body: <p>Before we go on, I know what you're thinking. <strong>I initialized my object</strong>. I also initialized in the appropriate method (I tried onCreateView and onResume). </p>
  
  <p>My problem is an object (actually, more than one object is throwing a Null Pointer, but they are initialized in the same way) that is throwing a Null Pointer Exception and I can't, for the life of me, figure out why.</p>
  
  <p><strong>Background:</strong> I have an Activity that contains a TabHost and a ViewPager. The Activity is also a container for a Fragment that I have (we'll call it Frag A). In my Activity when Frag A is visible the user can click a button that brings up two consecutive DialogFragments. When the second DialogFragment finishes, it calls a method from Frag A that adds some views to Frag A's layout. When the method is called, none of my objects remain initialized despite me trying to do so both in onCreateView and onResume.</p>
  
  <p>Below is applicable code: The exception is being thrown in the last method when the line <code>wpp.addBlankExercise();</code> is executed. I'm 99% sure that it has something to do with the way the TabHost/ViewPager is using lifecycle callbacks on Frag A. I don't know exactly what part of the TabHost/ViewPager is causing this though.</p>
  
  <p><strong>Activity</strong> </p>
  
  <pre><code>    static class DummyTabFactory implements TabHost.TabContentFactory {
      private final Context mContext;
  
      public DummyTabFactory(Context context) {
          mContext = context;
      }
  
      @Override
      public View createTabContent(String tag) {
          View v = new View(mContext);
          v.setMinimumWidth(0);
          v.setMinimumHeight(0);
          return v;
      }
  }
  
  public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
      super(activity.getSupportFragmentManager());
      mContext = activity;
      mTabHost = tabHost;
      mViewPager = pager;
      mTabHost.setOnTabChangedListener(this);
      mViewPager.setAdapter(this);
      mViewPager.setOnPageChangeListener(this);
  }
  
  public void addTab(TabHost.TabSpec tabSpec, Class&lt;?&gt; clss, Bundle args) {
      tabSpec.setContent(new DummyTabFactory(mContext));
      String tag = tabSpec.getTag();
  
      TabInfo info = new TabInfo(tag, clss, args);
      mTabs.add(info);
      mTabHost.addTab(tabSpec);
      notifyDataSetChanged();
  }
  
  @Override
  public int getCount() {
      return mTabs.size();
  }
  
  @Override
  public Fragment getItem(int position) {
      TabInfo info = mTabs.get(position);
      return Fragment.instantiate(mContext, info.clss.getName(), info.args);
  }
  
  @Override
  public void onTabChanged(String tabId) {
      int position = mTabHost.getCurrentTab();
      mViewPager.setCurrentItem(position);
  }
  
  @Override
  public void onPageSelected(int position) {
      TabWidget widget = mTabHost.getTabWidget();
      int oldFocusability = widget.getDescendantFocusability();
      widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
      mTabHost.setCurrentTab(position);
      widget.setDescendantFocusability(oldFocusability);
  }
  
  @Override
  public void getChoiceIndex(DialogFragment dialog) {
      amount = QuantityDialogFragment.amount;
      /* We keep track of which exercises are cardio exercises and which exercises are strength exercises here. If it is a cardio
      * exercise then the value is true (first choice) if it's not then the value is false (second example). */
      if (result == 0) {
          // Cardio exercise
          wpp.cardioBoolean = true;
      /* Null Pointer thrown here because the global variables that "addBlankExercise()" uses are no longer initialized. */
          wpp.addBlankExercise();
      } else {
          // Strength exercise
      // Null Pointer also thrown here for the same reason.
          wpp.cardioBoolean = false;
          wpp.addBlankExercise();
      }
  }
  </code></pre>
  
  <p>Here's a small part of <code>addBlankExercise()</code>. It is much longer than this, but it uses the basic pattern seen here:</p>
  
  <pre><code>public void addBlankExercise () {
      printLineNumber();
      System.out.println("addBlankExercise() " + getLineNumber());
      for (int i = 0; i &lt; WorkoutPlannerActivity.amount; i++) {
          printLineNumber();
          if (isCardioList.size() == etExerciseTracker) {
          isCardioList.add(cardioBoolean);
          }
          printLineNumber();
      LinearLayout layoutInfo = new LinearLayout(context);
      layoutInfo.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      layoutInfo.setOrientation(LinearLayout.HORIZONTAL);
      layoutInfo.setBackgroundColor(colorBlack);
      layoutInfo.setPadding(0, 0, 10, 5);
  
      editTextTitle = new EditText(context);
      editTextTitle.setHint("Sample Exercise");
      editTextTitle.setTextAppearance(context, R.style.style_exercise_title);
      editTextTitle.setHintTextColor(colorRed);
      editTextTitle.setBackgroundColor(colorAndroidTransparent);
      editTextTitle.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 12));
      editTextTitle.setTag(editTextTitle.getKeyListener());
      editTextTitle.setId(etExerciseTracker);
      etExerciseTracker ++;
  </code></pre>
  
  <p>Here's the stack trace:</p>
  
  <pre><code>02-08 07:26:46.082: E/AndroidRuntime(6790): FATAL EXCEPTION: main
  02-08 07:26:46.082: E/AndroidRuntime(6790): java.lang.NullPointerException
  02-08 07:26:46.082: E/AndroidRuntime(6790): at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
  02-08 07:26:46.082: E/AndroidRuntime(6790): at android.view.View.&lt;init&gt;(View.java:3234)
  02-08 07:26:46.082: E/AndroidRuntime(6790): at android.view.ViewGroup.&lt;init&gt; (ViewGroup.java:420)
  02-08 07:26:46.082: E/AndroidRuntime(6790): at android.widget.LinearLayout.&lt;init&gt; (LinearLayout.java:168)
  02-08 07:26:46.082: E/AndroidRuntime(6790): at com.nutrifit.Fragments.WorkoutPlannerPopup.addBlankExercise(WorkoutPlannerPopup.java:398)
  02-08 07:26:46.082: E/AndroidRuntime(6790): at com.nutrifit1.Activities.WorkoutPlannerActivity.getChoiceIndex(WorkoutPlannerActivity.java:198)
  02-08 07:26:46.082: E/AndroidRuntime(6790): at com.nutrifit.Fragments.QuantityDialogFragment$1.onClick(QuantityDialogFragment.java:44)
  </code></pre>
  
  <p>Thanks so much in advance for your help!</p>