title: Interface nullpointerexception in fragment onItemClickListener from gridview
body: <p>I'm using Fragments. I need the class extending FragmentActivity to implement interface that serves as listener from my fragments. When I click on an item from gridview, it fires onItemClick method, but the listener variable is null even though I set it in GamePlayActivity. </p>
  
  <p>My understanding is that I'm working on 2 different things when I instantiate my fragment to set listener and when onCreateView() is called on my fragment class.</p>
  
  <p>The sample from Google does the same implementation with onClick and it works. Not on mine. </p>
  
  <p><a href="https://github.com/playgameservices/android-samples/blob/master/TypeANumber/src/com/google/example/games/tanc/MainActivity.java" rel="nofollow">MainActivity extending FragmentActivity</a> and <a href="https://github.com/playgameservices/android-samples/blob/master/TypeANumber/src/com/google/example/games/tanc/MainMenuFragment.java" rel="nofollow">Fragment class</a> for example.</p>
  
  <p>Fragment 1</p>
  
  <pre><code>public class FragmentOne extends Fragment implements OnItemClickListener {
  
      Listener listener = null;
  
      interface Listener {
          public void applyGameLogic();
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          View layout = inflater.inflate(R.layout.fragment_fragpane, container, false);
          GridView gridView = (GridView) layout.findViewById(R.id.grid);
          gridView.setAdapter(new ImageAdapter(layout.getContext()));
  
          gridView.setOnItemClickListener(this);
  
          return layout;
      }
  
      public void setListener(Listener listener) {
          this.listener = listener;
      }
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
          //listener.applyGameLogic(); this listener is null
      }
  }
  </code></pre>
  
  <p>GamePlayActivity</p>
  
  <pre><code>public class GamePlayActivity extends FragmentActivity implements WordPane.Listener, ChainPane.Listener {
  
      private FragmentOne fragment1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
              setContentView(R.layout.game_container);
  
          fragment1 = new FragmentOne();
  
          fragment1.setListener(this);
      }
  
      ... applyGameLogic method follows but its empty for now
  }
  </code></pre>