title: Fragments with Loader (works on Tablet, but not on Handset -> Error not attached to Activity)
body: <p>I have two fragments are supposed to work like in the <a href="http://developer.android.com/images/fundamentals/fragments.png" rel="nofollow noreferrer">picture</a>.</p>
  
  <p><img src="https://i.stack.imgur.com/wHlst.png" alt="enter image description here"></p>
  
  <p>With a Tablet AVD it works perfectly, but when I use a handset AVD always comes this error message:</p>
  
  <pre><code> 10-15 07:31:46.450: E/AndroidRuntime(721): FATAL EXCEPTION: main
   10-15 07:31:46.450: E/AndroidRuntime(721): java.lang.IllegalStateException: Fragment EventsFragment{411e6440 id=0x7f070005} not attached to Activity
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.support.v4.app.Fragment.getLoaderManager(F ragment.java:823)
   10-15 07:31:46.450: E/AndroidRuntime(721): at com.example.activity.EventsFragment.updateEventsVi ew(EventsFragment.java:80)
   10-15 07:31:46.450: E/AndroidRuntime(721): at com.example.activity.EventsActivity.onYearSelected (EventsActivity.java:79)
   10-15 07:31:46.450: E/AndroidRuntime(721): at com.example.activity.YearsFragment.onListItemClick (YearsFragment.java:63)
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.support.v4.app.ListFragment$2.onItemClick( ListFragment.java:58)
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.widget.AdapterView.performItemClick(Adapte rView.java:298)
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.widget.AbsListView.performItemClick(AbsLis tView.java:1086)
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.widget.AbsListView$PerformClick.run(AbsLis tView.java:2859)
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.widget.AbsListView$1.run(AbsListView.java: 3533)
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.os.Handler.handleCallback(Handler.java:615 )
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.os.Handler.dispatchMessage(Handler.java:92 )
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.os.Looper.loop(Looper.java:137)
   10-15 07:31:46.450: E/AndroidRuntime(721): at android.app.ActivityThread.main(ActivityThread.jav a:4745)
   10-15 07:31:46.450: E/AndroidRuntime(721): at java.lang.reflect.Method.invokeNative(Native Method)
   10-15 07:31:46.450: E/AndroidRuntime(721): at java.lang.reflect.Method.invoke(Method.java:511)
   10-15 07:31:46.450: E/AndroidRuntime(721): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:786)
   10-15 07:31:46.450: E/AndroidRuntime(721): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:553)
   10-15 07:31:46.450: E/AndroidRuntime(721): at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>used AVDs:
  Tablet: Platform: 3.0 API Level: 11
  Handset: Platform: 4.1.2 API Level: 16</p>
  
  <p>EventsActivity.java</p>
  
  <pre><code>public class EventsActivity extends FragmentActivity implements YearsFragment.OnYearSelectedListener{
  
  @Override
  protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);
      setContentView(R.layout.years_events);
  
      if (findViewById(R.id.fragment_container) != null) { //Handset
          if (savedInstanceState != null) {
              return;
          }
  
          //Fragment A
          YearsFragment firstFragment = new YearsFragment(); 
          firstFragment.setArguments(getIntent().getExtras());
  
          getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
      }
  }
  
  @Override
  public void onYearSelected(Years year) {
      EventsFragment eventFrag = (EventsFragment) getSupportFragmentManager().findFragmentById(R.id.event_fragment);
  
      if (eventFrag != null) { //Tablet
          //update Fragment B
          eventFrag.updateEventsView(year);
      } else { //Handset
          //start Fragment B
          EventsFragment newFragment = new EventsFragment();          
          newFragment.updateEventsView(year);
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
          transaction.replace(R.id.fragment_container, newFragment);
          transaction.addToBackStack(null);
          transaction.commit();
      }
  }}
  </code></pre>
  
  <p>EventsFragment (Fragment B)</p>
  
  <pre><code>public class EventsFragment extends ListFragment implements LoaderManager.LoaderCallbacks&lt;List&lt;Events&gt;&gt;{
  EventsAdapter adapter;
  private int year;
  
  OnEventSelectedListener mCallback;
  public interface OnEventSelectedListener {
      public void onEventSelected(Events event);
  }
  
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  
  
      adapter = new EventsAdapter(getActivity());
      setListAdapter(adapter);
  
      setEmptyText("No Data Here");
      setListShown(false);
  
      year = Calendar.getInstance().get(Calendar.YEAR);
      getLoaderManager().initLoader(year, null, this);
  }
  
  @Override
  public void onAttach(Activity activity){
      super.onAttach(activity);
  
      try{
          mCallback = (OnEventSelectedListener) activity;
      } catch (ClassCastException e){
          throw new ClassCastException(activity.toString() + "must implement OnEventsSelectedListener");
      }
  }
  
  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
      mCallback.onEventSelected(adapter.getItem(position));
  }
  
  public void updateEventsView(Years year) {
      this.year = Integer.parseInt(year.getYear());
      getLoaderManager().initLoader(this.year, null, this); //TODO error  }
  
  //Loader
  @Override
  public Loader&lt;List&lt;Events&gt;&gt; onCreateLoader(int arg0, Bundle arg1) {
      return new EventsLoader(getActivity(),this.year);
  }
  
  @Override
  public void onLoadFinished(Loader&lt;List&lt;Events&gt;&gt; arg0, List&lt;Events&gt; data) {
      adapter.setData(data);
  
      if(isResumed()){
          setListShown(true);
      }else{
          setListShownNoAnimation(false);
      }
  }
  
  @Override
  public void onLoaderReset(Loader&lt;List&lt;Events&gt;&gt; arg0) {
      adapter.setData(null);
  }
  </code></pre>
  
  <p>Why it doesn't work with the Handset?</p>
  
  <p>greets Mr.P.</p>